0

使用 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(SessionMode = SessionMode.Allowed, Namespace = "CS.ServiceContracts.Zzzzzz.Common",
    Name = "IZzzzzzCommonService")]
public interface IZzzzzzCommonService
{
    [OperationContract]
    GetZipCodesResponse GetZipCodes(GetZipCodesRequest request);
}

这是我的数据合同:

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

这是响应库:

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

   }

我已经调试了代码,我正在进入实体,并且数据集填充了适当的行,但是当它发送回数据集时,我得到了错误。

不知道我错过了什么。我相信我已经正确装饰了 serviceContract 和 dataContract 成员,所以不确定为什么它在序列化列表时遇到问题。

4

1 回答 1

0

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

于 2013-07-25T18:47:21.453 回答