2017-04-05 91 views
2

此問題未重複,請仔細閱讀。收到HTTP響應時發生WCF錯誤

我有一個WCF服務和客戶端,在同一個域上的不同機器上。 WCF服務具有以下幾種操作合同:

[ServiceContract] 
public interface IPatchService 
{ 
    [OperationContract] 
    List<PatchUpdateDTO> SearchForUpdates(); 

    [OperationContract] 
    string InstallUpdates(); 
} 

PatchUpdateDTO類:

[Serializable] 
[DataContract] 
public class PatchUpdateDTO 
{ 
    [DataMember] 
    private string UpdateId { get; } 
    [DataMember] 
    private string Title { get; } 
    [DataMember] 
    private string Description { get; } 
} 

的請求監聽的服務代碼:

Uri baseAddress = new Uri("http://localhost:8000/PatchManagementService"); 
    _selfHost = new ServiceHost(typeof(PatchService), baseAddress); 

    try 
    { 
     _selfHost.AddServiceEndpoint(typeof(IPatchService), new WSHttpBinding(), "PatchService"); 

     ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
     smb.HttpGetEnabled = true; 
     _selfHost.Description.Behaviors.Add(smb); 
     _selfHost.Open(); 
    } 
    catch (CommunicationException ce) 
    { 
    ... 
    } 

我已經在我的客戶端添加服務引用應用。

客戶端代碼:

public class AgentCommunicationWCFProvider : IAgentComminicationProvider 
{ 
    private readonly PatchServiceClient _patchManagementService; 

    public AgentCommunicationWCFProvider() 
    { 
     _patchManagementService = new PatchServiceClient(); 
    } 
    public string InstallUpdates() 
    { 
     return _patchManagementService.InstallUpdates(); 
    } 

    public List<PatchUpdateDTO> SearchForUpdates() 
    { 
     return _patchManagementService.SearchForUpdates(); 
    } 

}

客戶端配置:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WSHttpBinding_IPatchService" /> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://someIpAddress:8000/PatchManagementService/PatchService" 
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPatchService" 
      contract="PatchManagementServiceReference.IPatchService" name="WSHttpBinding_IPatchService"> 
     <identity> 
      <userPrincipalName value="****" /> 
     </identity> 
     </endpoint> 
    </client> 
    </system.serviceModel> 

現在一些奇怪的是發生,使用InstallUpdates()方法時,我得到一個返回字符串和它工作正常,但是當使用SearchForUpdates()方法時,假設返回List<PatchUpdateDTO>,我得到以下錯誤:

附加信息:接收HTTP響應時發生錯誤 http:// someIpAddress:8000/PatchManagementService/PatchService。 這可能是由於服務端點綁定不使用HTTP協議。 這也可能是由於HTTP請求上下文被服務器 中止(可能是由於服務關閉)。查看服務器日誌獲取更多詳細信

任何想法,爲什麼它有一個複雜的對象的問題?

+0

擺脫[Serializable]屬性並重試。你已經在類和屬性上有了DataContract和DataMember屬性,那麼你爲什麼要在頂部使用[Serializable]?這個問題似乎在這個區域,因爲InstallUpdates()只是回退一個WCF默認可以理解的字符串。 –

+0

爲什麼您的PatchUpdateDTO類具有私有屬性?他們應該是公開的。 –

回答

1

好了 - 我剛纔抄了你的代碼,發現了以下變化:

  1. 擺脫[Serializable]屬性。 如果你想使用DataContract/DataMember屬性,否則保留它,但刪除DataContract/DataMember,並且所有屬性將自動成爲 serialisable。
  2. 更改訪問修飾符'private'public PatchUpdateDTO類的所有屬性,否則您將無法在客戶端 應用程序中訪問它們。
  3. 二傳手添加到 PatchUpdateDTO類的所有屬性,否則您將無法設置PatchUpdateDTO
  4. 在客戶端配置之外的價值觀,改變someIpAddress到本地主機或IP地址。 WCF服務正在運行的機器的地址。

上述更改後,它在我的機器上工作並返回PatchUpdateDTO對象的列表。

希望這可以解決您的問題。

+0

謝謝你,從私人到公共的修改變化使它成功 –

相關問題