WCF服务无法将自定义类(包含Exception类型作为成员)序列化为操作契约中的返回类型

时间:2013-11-01 13:04:55

标签: c# wcf exception service client

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

编辑:

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

编辑2: 好的,现在我像nvoigt那样修改了我的课程。但遗憾的是,这并没有解决我的问题,我仍然收到警告,仍然没有创建服务客户端。

以下是修改后的代码:

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

这是我的自定义AsyncCallResponse类:

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继承的操作契约的返回类型:

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个警告,这是他们:

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

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

我希望任何人都可以帮助我,有点烦恼这个问题,我不知道解决方案。 在此先感谢^^

1 个答案:

答案 0 :(得分:0)

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

查看使用指南here

相关问题