2013-07-25 52 views
0

使用VS2012和NetTcpBinding。我從客戶端調用serviceContract時遇到以下錯誤 - 服務託管在IIS中:使用NetTCP的WCF序列化錯誤

嘗試序列化參數CS.ServiceContracts.Zzzzzz.Common時發生錯誤:GetZipCodesResult。 InnerException消息是'類型'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); 
      } 
     } 

    } 

我已經調試代碼,我進入了實體和數據集填充了相應的行,但是當它發出數據集返回是當我得到錯誤。

不知道我缺少什麼。我相信我有serviceContract和dataContract成員正確裝飾,所以不知道爲什麼它有序列化列表的問題。

+0

您能否提供ZipCodes類的代碼 –

回答

0

問題是ZipCode類。這是一個實體類,它不能被序列化。所以我解構了班級,並且能夠將它發回給客戶。