WCF – An existing connection was forcibly closed by the remote host

WCF – An existing connection was forcibly closed by the remote host

If you’ve run in to the following error message in your WCF service:

System.ServiceModel.CommunicationException:    An error occurred while receiving the HTTP response to https://YourDomain.com/Service.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.

You most likely already spent many hours trying to debug the exception. This happen to me and I searched Google and read through tons of threads, most about updating the following attributes in Web.Config/App.Config on the Server/Client:

  • maxReceivedMessageSize
  • maxStringContentLength
  • maxBufferSize
  • maxArrayLength
  • maxBytesPerRead
  • maxDepth

Others recommended enabling tracing on the client and on the server. None of these suggestions helped me… So I sat and banged my head against the wall for a few more hours and thought “What was different about this particular service method as compared to my other methods in my WCF service?“.

It turns out, my issues was actually really simple to resolve but the error message had nothing to do with the actual issue at hand. All the methods in my WCF service return custom entities (classes) and in this particular method one of the properties was of type DataTable.

The issue all along was that I wasn’t giving the DataTable a Name.

DataTable dt = new DataTable(“TheName”);

I hope this helps you guys save sometime.

Happy coding! 🙂