WCF Service Cannot serialize a custom class (which contains an exception type as a member) as a return type in a work contract

First of all, I found several topics, but I did not feel that they were relevant to my problem.

Edit:

I forgot to say that when I add a service link to a Silverlight project, I forbid using reuse types ... in the advanced settings.

Edit 2: Ok, now I changed my classes as nvoigt said. But, unfortunately, this did not solve my problem, I still get warnings and still do not create a service client.

Here is the modified code:

I have a Silverlight project that uses a WCF service to manage a database.

Here is my custom AsyncCallResponse class:

using System; namespace OnlineButoraruhaz.Web.Response { [DataContract] public class AsyncCallResponse { public AsyncCallResponse() { } public AsyncCallResponse(bool hasSucceeded, Exception error) { HasSucceeded = hasSucceeded; Error = error; } [DataMember] public bool HasSucceeded { get; set; } [DataMember] public Exception Error { get; set; } } } 

Here is my custom class, which should be my return type of operation contract, inheriting from AsyncCallResponse:

 using System; using System.Collections.Generic; using OnlineButoraruhaz.Web.Model; namespace OnlineButoraruhaz.Web.Response.Furniture { [DataContract] public class GetFurnitureListResponse : AsyncCallResponse { [DataMember] public IEnumerable<FurnitureEntityDto> FurnitureList { get; set; } public GetFurnitureListResponse() { } public GetFurnitureListResponse(IEnumerable<FurnitureEntityDto> furnitureList, bool hasSucceeded = true, Exception error = null) : base(hasSucceeded, error) { FurnitureList = furnitureList; } } } 

Here is my service interface:

 using System.ServiceModel; using OnlineButoraruhaz.Web.Model; using OnlineButoraruhaz.Web.Response.Furniture; namespace OnlineButoraruhaz.Web { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IFurnitureService" in both code and config file together. [ServiceContract] public interface IFurnitureService { [OperationContract] GetFurnitureListResponse GetFurnitureList(); //the rest of my code... } } 

Here is the service implementation:

 using System; using System.Data; using System.Linq; using OnlineButoraruhaz.Web.Model; using OnlineButoraruhaz.Web.Response.Furniture; namespace OnlineButoraruhaz.Web { public class FurnitureService : IFurnitureService { private readonly FurnitureDataBaseEntities _db = new FurnitureDataBaseEntities(); public GetFurnitureListResponse GetFurnitureList() { GetFurnitureListResponse result; try { var query = (from f in _db.FURNITUREs select new FurnitureEntityDto { FurnitureId = f.FurnitureID, Name = f.Name, Type = f.Type, Color = f.Color, Width = f.Width, Height = f.Height, Depth = f.Depth, Image = f.Image, Cover = f.Cover, Structure = f.Structure, Price = f.Price, OnStock = f.OnStock, RowVersion = f.RowVersion }); result = new GetFurnitureListResponse(query); } catch (Exception ex) { result = new GetFurnitureListResponse(null, false, ex); } return result; } //the rest of my code } } 

So my problem is, when I try to add a ServiceReference to my Silverlight project, it creates but does not create a service client. I get 4 warnings, here they are:

 Warning 1 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: ISerializable type with data contract name 'Exception' in namespace 'http://schemas.datacontract.org/2004/07/System' cannot be imported. The data contract namespace cannot be customized for ISerializable types and the generated namespace 'OnlineButoraruhaz.FurnitureServiceReference' does not match the required CLR namespace 'System'. Check if the required namespace has been mapped to a different data contract namespace and consider mapping it explicitly using the namespaces collection. XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='IFurnitureService'] D:\Workspace\SVN\OnlineFurnitureStore\OnlineButoraruhaz\Service References\FurnitureServiceReference\Reference.svcmap 1 1 OnlineButoraruhaz Warning 2 Custom tool warning: Cannot import wsdl:binding Detail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on. XPath to wsdl:portType: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='IFurnitureService'] XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:binding[@name='BasicHttpBinding_IFurnitureService'] D:\Workspace\SVN\OnlineFurnitureStore\OnlineButoraruhaz\Service References\FurnitureServiceReference\Reference.svcmap 1 1 OnlineButoraruhaz Warning 3 Custom tool warning: Cannot import wsdl:port Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on. XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:binding[@name='BasicHttpBinding_IFurnitureService'] XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:service[@name='FurnitureService']/wsdl:port[@name='BasicHttpBinding_IFurnitureService'] D:\Workspace\SVN\OnlineFurnitureStore\OnlineButoraruhaz\Service References\FurnitureServiceReference\Reference.svcmap 1 1 OnlineButoraruhaz Warning 4 Custom tool warning: No endpoints compatible with Silverlight 5 were found. The generated client class will not be usable unless endpoint information is provided via the constructor. D:\Workspace\SVN\OnlineFurnitureStore\OnlineButoraruhaz\Service References\FurnitureServiceReference\Reference.svcmap 1 1 OnlineButoraruhaz 

My main role is not to directly get the furniture list by the service, but to get a furniture sheet that contains the furniture list, hasSucceeded a logical value and an exception if the operation failed.

I hope someone can help me, it is annoying if I have this problem and I have no idea about the solution. Thanks in advance ^^

0
source share
1 answer

All classes carried by WCF (input and return) need a DataContract and DataMember attributes for proper serialization.

Look here for a usage guide.

0
source

Source: https://habr.com/ru/post/1012025/


All Articles