WCF服务无法将自定义类(其中包含Exception类型作为成员)序列化为操作协定中的返回类型 [英] WCF Service Cannot serialize custom class (which contains an Exception type as member) as a return type in operation contract

查看:118
本文介绍了WCF服务无法将自定义类(其中包含Exception类型作为成员)序列化为操作协定中的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我找到了几个主题,但是我觉得它们与我的问题无关。

First of all, I have found several topics, but I've not felt any of them relevant to my problem.

编辑:

我忘了说,当我将服务引用添加到Silverlight项目时,我取消了复用类型...复选框的高级设置。

I forgot to say, when I am adding the Service Reference to the Silverlight project, I am unticking Reuse types... checkbox under advanced settings.

编辑2:
好​​的,现在我像nvoigt所说的那样修改了类。不幸的是,这并没有解决我的问题,我仍然收到警告,仍然没有创建服务客户端。

Edit 2: Ok, now I modified my classes like nvoigt said. But sadly this didn't solve my problem, I am still getting the warnings, and still not creating the service client.

这是修改后的代码:

我有一个Silverlight项目,该项目使用WCF服务来管理数据库。

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

这是我自定义的AsyncCallResponse类:

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; }
    }
}

这是我的自定义类,应该作为我的回报从AsyncCallResponse继承的操作合同的类型:

Here is my custom class that should be my return type of the operation contract inheriting from the 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;
        }
    }
}

这是我的服务界面:

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...
    }
}

这里是服务的实现:

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
    }
}

所以,我的问题是,当我尝试在Silverlight项目中添加ServiceReference时,它会创建,但是无法创建服务客户端。
我收到4条警告,它们是:

So, my problem, is when I am trying to add ServiceReference in my Silverlight project, it creates, but fails to create the service client. I get 4 warnings, here are them:

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

我的主要职责是不直接通过服务获取家具列表,而是获取一个家具列表响应,其中包含家具的列表,hasSucceeded逻辑值和一个异常(如果操作失败)。

My main role is to not directly get the Furniture's list by the service, but get a FurnitureListResponse which contains the furniture's list, a hasSucceeded logical value, and an exception, if the operation fails.

我希望有人能帮助我,遇到这个问题有点烦人,我不知道解决方案。
预先感谢^^

I hope anyone can help me, it's kindda annoying having this problem, and I have no idea about the solution. Thanks in advance ^^

推荐答案

由WCF传输的所有类(输入和返回)都需要 DataContract 和DataMember属性为正确序列化。

All classes transported by WCF (input and return) need DataContract and DataMember attributes to be serialized correctly.

看看这里以获取使用指南。

这篇关于WCF服务无法将自定义类(其中包含Exception类型作为成员)序列化为操作协定中的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆