Search code examples
c#wcfwcf-binding

WCF error occurred while receiving the HTTP response


This question is not duplicated, please read this carefully.

I have a WCF service and a client, on different machines on the same domain. The WCF service has the following operation contracts:

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

    [OperationContract]
    string InstallUpdates();
}

PatchUpdateDTO class:

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

The service code for requests listener:

    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)
    {
    ...
    }

I have added the service reference in my client application.

The client code :

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

}

The client configuration:

  <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>

Now something odd is happening, when using the InstallUpdates() method I get a return string and it is working fine, but when using the SearchForUpdates() method, that suppose to return List<PatchUpdateDTO>, I get the following error:

Additional information: An error occurred while receiving the HTTP response to http://someIpAddress:8000/PatchManagementService/PatchService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Any ideas why it has a problem with a complex object?


Solution

  • Okay - I have just copied your code and found the following changes:

    1. Get rid of [Serializable] attribute. If you want to use DataContract/DataMember attributes, otherwise keep it but delete DataContract/DataMember and all properties will autometically be serialisable.
    2. Change the access modifier 'private' to public for all properties of PatchUpdateDTO class otherwise you wont be able to access them in the client application.
    3. Add the setter set; to all the properties of PatchUpdateDTO class otherwise you wont be able to set the values outside of PatchUpdateDTO class
    4. On the client config, change someIpAddress to the localhost or to the IP. address of the machine where WCF service is running.

    After above changes it works on my machine and returns the list of PatchUpdateDTO objects.

    Hope this solves your problem.