45

I have a WCF web service that is working fine. However there is one particular call that is failing - but only failing for certain users. The call is pretty simple - it is a call to get a list of Person objects.

For User A it works fine. The service queries the database, creates the list of Person objects and returns it back to the calling application.

For User B it fails. The weird thing is that when I do debugging the service seems to work fine. It is able to query the database and it creates the List object and returns it. The service itself never fails. But the client application receives the "An existing connection was forcibly closed by the remote host" error.

To me it seems like something is happening when the service layer is trying to package up the data in XML format to send back to the calling application. I am thinking that it has to be a data related problem because the call works fine for other users. I have visually looked at the data and I don't really see anything odd. One guess is that the data for User B has some funky hidden characters or something and therefore is causing the service to close unexpectedly. Something like that.

Any ideas?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Corey Burnett
  • 7,312
  • 10
  • 56
  • 93
  • Maybe it helps - I had deleted the web service application from server while the service was being called from clients and I got this error in my error logs.. – Zameer Ansari Jul 10 '14 at 15:33

11 Answers11

73

The best thing I've found for diagnosing things like this is the service trace viewer. It's pretty simple to set up (assuming you can edit the configs):

http://msdn.microsoft.com/en-us/library/ms732023.aspx

Hope this helps.

Liam
  • 27,717
  • 28
  • 128
  • 190
NateTheGreat
  • 2,295
  • 13
  • 9
  • 12
    This helped! I had a similar problem and in my case, the ServiceTraceViewer showed me an exception being thrown while trying to serialize an enum value. The operation was trying to return an object that includes this enum without initalizing it first, and the enum definition had no member for the default value (0). So it could not serialize 0. – Eren Ersönmez Oct 26 '11 at 08:08
  • I did this and it enabled me to pinpoint the issue. In my case it was a NullReferenceException during serialization back to the client. Here's what I want to know: Why was my original exception buried in this connection issue? It was not even any of the inner exceptions, but it was the cause of the issue. This is retarded and I need to come up with a way to overcome this. I may post a new question idk... – toddmo Sep 10 '15 at 16:13
23

I had this issue because my website did not have a certificate bound to the SSL port. I thought I'd mention it because I didn't find this answer anywhere in the googleweb and it took me hours to figure it out. Nothing showed up in the event viewer, which was totally awesome for diagnosing it. Hope this saves someone else the pain.

Neuralsim
  • 231
  • 2
  • 2
11

I have seen this once. Are the users requesting different amounts of data? I found that even if you can configure a binding for data payloads (i.e. maxReceivedMessageSize), the httpRuntime maxRequestLength trumps the WCF setting, so if IIS is trying to serve a request that exceeds that, it exhibits this behavior.

Think of it like this:

If maxReceivedMessageSize is 12MB in your WCF behavior, and maxRequestLength is 4MB (default), IIS wins.

AlG
  • 14,697
  • 4
  • 41
  • 54
Ta01
  • 31,040
  • 13
  • 70
  • 99
  • 2
    I don't think that is it. I tried modifying the data that was returned to reduce it and it didn't work. The normal query returns 63 rows. I changed it to return only 1 row and it still fails. The data for User A returns 26 rows and it works fine. When I modify the data for User B to return only 1 row - it still fails. Very strange. – Corey Burnett Aug 26 '11 at 20:38
8

I found that you can get this error if the returned object has getter only auto properties that are initialized in the constructor (with C# 6.0 syntax).

I believe this is due to WCF deserializing objects on the client side using a parameter-less constructor then setting the properties on the object. It needs to have a set available (it can be private) to fill the object, otherwise it'll fail.

Philippe
  • 3,945
  • 3
  • 38
  • 56
  • 1
    O....My....God.... thank you so much for this answer. I wonder why this isn't mentioned or caught at compile time. – DdW Apr 06 '17 at 11:07
6

I just had this error now in server only and the solution was to set a maxItemsInObjectGraph attribute in wcf web.config under <behavior> tag:

<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
ParPar
  • 7,355
  • 7
  • 43
  • 56
3

I have catched the same exception and found a InnerException: SocketException. in the svclog trace.

After looking in the windows event log I saw an error coming from the System.ServiceModel.Activation.TcpWorkerProcess class.

Are you hosting your wcf service in IIS with netTcpBinding and port sharing?

It seems there is a bug in IIS port sharing feature, check the fix:

My solution is to host your WCF service in a Windows Service.

  • This is for an old version of IIS, like 6? Since the "fix" is from 2011 and the download link is even gone... – Serj Sagan Sep 10 '15 at 00:20
3

I' ve got the same problem. My solution is this :

If you using LinQ2SQL in your project, Open your dbml file in Visual Studio and change Serialization Mode to "Unidirectional" on

gürcan
  • 31
  • 1
3

After pulling my hair out for like 6 hours of this completely useless error, my problem ended up being that my data transfer objects were too complex. Start with uber simple properties like public long Id { get; set;} that's it... nothing fancy.

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
3

In my case it was also with serialization. I need to add [KnownType(typeof(...)] for all the classes that could appear in the serialization.

Hans S
  • 101
  • 2
  • 3
2

The issue I had was also with serialization. The cause was some of my DTO/business classes and properties were renamed or deleted without updating the service reference. I'm surprised I didn't get a contract filter mismatch error instead. But updating the service ref fixed the error for me (same error as OP).

goku_da_master
  • 4,257
  • 1
  • 41
  • 43
0

I had this issue start happening when debugging from one web project to a web service in the same solution. The web service was returning responses that the web project couldnt understand. It would kind of work again at some points, then stop again.

It was because there was not an explicit reference between these projects, so the web service was not getting built when hitting F5 to start debugging. Once I added that, the errors went away.

StingyJack
  • 19,041
  • 10
  • 63
  • 122