Question

I have a simple WCF service with two OperationContracts:

public IEnumerable<string> GetDrivers()
    {
        return new List<string>() { "Senna", "Mansell", "Prost" };
    }

    public IEnumerable<DriverDto> GetRealDrivers()
    {
        using (var ctx = new F1Entities())
        {
            var result = from d in ctx.Drivers select new DriverDto { Name = d.Name, Cost = d.Cost, Team = d.Team };
            return result;

        }
    }

The F1Entities object is a dbContext for an Azure SQL db. DriverDto class has the DataContract attribute, and Name, Team and Cost have DataMember attributes

When debugging the service locally, and using the WCF Test Client, I get a correct response from GetDrivers. When I call GetRealDrivers though I get an error.

An error occurred while receiving the HTTP response to http://localhost:58059/Service1.svc. 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. Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) at System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at IF1Service.GetRealDrivers() at F1ServiceClient.GetRealDrivers() Inner Exception: The underlying connection was closed: An unexpected error occurred on a receive. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) Inner Exception: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead) Inner Exception: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

Debugging the service I see that it is correctly using the context and populating the result variable with objects of type DriverDto - all with the correct values for name, cost and team. So it is connecting to the SQL db just fine.

I have seen a lot of questions raised with this error, but having spent many hours following them all I feel I have missed something more obvious.

Adding logging I find exception errors in the log like this:

Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; charset=utf-8. The client and service bindings may be mismatched.

And spending more hours trying to get to the bottom error of that has resulted in brain meltdown. My Web.config for the service doesn't have any binding attributes but still works for the simple GetDrivers call. Am I missing something in my web config? This was created using the VS2013 template for Azure Cloud Service.

Was it helpful?

Solution

You would have to store them in memory first with a ToList() for example, because the context is disposed before the serialization is complete. For debugging purposes you can add includeExceptionDetailsInFault=true to the servers web.config.

Example:

public IEnumerable<DriverDto> GetRealDrivers()
{
    using (var ctx = new F1Entities())
    {
        var result = from d in ctx.Drivers
                     select new DriverDto 
                     {
                         Name = d.Name,
                         Cost = d.Cost,
                         Team = d.Team
                     };
        return result.ToList();
    }
}

Web.Config:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top