使用NetTCP的WCF序列化错误

时间:2013-07-25 15:13:47

标签: wcf

使用VS2012和NetTcpBinding。当我从客户端调用serviceContract时出现以下错误 - 该服务托管在IIS中:

尝试序列化参数CS.ServiceContracts.Zzzzzz.Common时出错:GetZipCodesResult。 InnerException消息是“Type”System.DelegateSerializationHolder + DelegateEntry',数据协定名称为“DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System”,不是预期的。考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。有关详细信息,请参阅InnerException。

这是我的ServiceContract:

  [ServiceContract(SessionMode = SessionMode.Allowed, Namespace = "CS.ServiceContracts.Zzzzzz.Common",
    Name = "IZzzzzzCommonService")]
public interface IZzzzzzCommonService
{
    [OperationContract]
    GetZipCodesResponse GetZipCodes(GetZipCodesRequest request);
}

这是我的DataContract:

[DataContract]
[Serializable]
public class GetZipCodesResponse : ResponseBase
{
    [DataMember(IsRequired = true)]
    public List<ZipCodes> ZipCodes { get; set; }
}

这是ResponseBase:

[DataContract]
[Serializable]
[KnownType(typeof(GetZipCodesResponse)), XmlInclude(typeof(GetZipCodesResponse))]
public class ResponseBase
{
    [DataMember(IsRequired = true)]
    public int ResponseCode { get; set; }

    [DataMember(IsRequired = false)]
    public int ReasonCode { get; set; }

    [DataMember(IsRequired = false)]
    public string ReasonText { get; set; }
}

以下是serviceContract接口的实现:

public class ZzzzzzCommonService : IZzzzzzCommonService
{
 public GetZipCodesResponse GetZipCodes(GetZipCodesRequest request)
    {
        var response = new GetZipCodesResponse();
        try
        {
            response.ZipCodes = ZipCodes.GetCustom(request.ZipCode, request.City, request.State);

        }
        catch (Exception ex)
        {
            this.BuildExceptionResponse(response, ex);
        }

        return response; // I get to this line ok, but here is where the error occurs
    }
}

以下是我称之为服务的客户端代码:

 public void ZipCodes()
    {
        var endPoint = new EndpointAddress(
            "net.tcp://localhost/CS.WebService.Zzzzzz.Common/ZzzzzzCommonService.svc");

        var binding = new NetTcpBinding { TransferMode = TransferMode.Buffered, SendTimeout = TimeSpan.MaxValue, ReceiveTimeout = TimeSpan.MaxValue, MaxReceivedMessageSize = 100000000, MaxBufferSize = 100000000 };

        using (var channel = new ChannelFactory<IZzzzzzCommonService>(binding, endPoint))
        {
            try
            {
                channel.Endpoint.Contract.SessionMode = SessionMode.Allowed; 
                var proxy = channel.CreateChannel();
                var request = new GetZipCodesRequest();

                request = new GetZipCodesRequest { ZipCode = "32701" };
                response = proxy.GetZipCodes(request);
            }
        }

   }

我已经调试了代码,我进入了实体,并且dataSet填充了相应的行,但是当它发回数据集时,就是我收到错误。

不确定我错过了什么。我相信我已正确装饰serviceContract和dataContract成员,因此不确定为什么序列化List时出现问题。

1 个答案:

答案 0 :(得分:0)

问题是ZipCode类。它是一个Entity类,无法序列化。所以我解构了这个课,它能够把它发回给客户。