15

I have a web service which is returning data to the desktop application. The problem I am having is, when the web service returns small volume of data everything works fine but when the volume of data is large it throws the following exception:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.

And when I am debugging the web service, I see that this particular method is called twice. It executes the return statement 1st time nothing happens, but when it does execute it for the second time the above mentioned exception is thrown in the desktop app.

I found similar posts before on stackoverflow but they did not solve my problem. Can anybody please tell me what's going on in here?

Thanks!

Saurabh Lalwani
  • 705
  • 2
  • 7
  • 15
  • Are you receiving this exception on the client or on the server? I think it's on the client. In that case, you need to look in the Windows event log to see what happens in the server. You could also turn on WCF tracing to see what's going on in the service. – John Saunders Apr 19 '10 at 18:34
  • There is nothing in the Windows event log for this exception. Could you please help me how to trace the Web service. I have never done that before. Even if you could forward me a link for the tracer it will be of great help. Thanks – Saurabh Lalwani Apr 19 '10 at 18:52

7 Answers7

12

It could be because the size of the message is greater than the default message size. You might try increasing the this value in the configuration of the endpoint. You could also take a look at this post.


UPDATE:

To further diagnose the problem I would suggest you activating the trace on the service by putting the following in the config file:

<system.diagnostics>
    <trace autoflush="true">
    </trace>
    <sources>
        <source name="System.ServiceModel"
                switchValue="Information, ActivityTracing"
                propagateActivity="true">
            <listeners>
                <add name="sdt"
                     type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="WcfDetailTrace.e2e" />
            </listeners>
        </source>
    </sources>
</system.diagnostics>

This will generate the WcfDetailTrace.e2e trace file which you could open with the Service Trace Viewer Tool which will provide you with extensive information about the call and the error message.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The size is not the problem. I am setting the size to 16 MB and the data being transferred is less than that. I already did look at both the mentioned posts, but did not seem to help. Any other suggestions please? Thanks.. – Saurabh Lalwani Apr 19 '10 at 17:43
  • 1
    @DarinDimitrov This one saved my life and project, couldn't figure out what was wrong. Found in the trace that EF-Entity is not a data contract. Thanks! :) – Hitin Aug 24 '16 at 08:20
  • @DarinDimitrov .. this link is unavailable, could you please update your answer, stackoverflow does't prefer link, so try to add a breif for link + link – Mohamad Mahmoud Darwish Oct 04 '17 at 08:03
6

I recently had this issue.

It turned out that analyzing the WCF log as written by the System.Diagnostics.XmlWriterTraceListener yielded a problem with the data contract I had set up.

I am returning Dictionary<int, object> (Side Note: Yes, I know this is really bad!, but I am young and need the money). I forgot to include the [KnownType] attribute on the return value for the DataContract:

    [DataContract]
    [KnownType(typeof(Dictionary<int, double>))]
    [KnownType(typeof(Dictionary<int, ChannelData>))]
    [KnownType(typeof(Dictionary<string, Dictionary<int, double>>))]
    public class MyCoolObject: ICoolObject
    {
[DataMember]
        public Dictionary<string, object> Results
        {
            get { return _results; }
            set { _results = value; }
        }
    }
Batgar
  • 1,519
  • 2
  • 15
  • 17
5

I too had this issue. For me, it was happening because I had a [DataMember] property with a get{} but no set{}. After adding a set{} this behavior stopped.

phillipwei
  • 1,243
  • 2
  • 12
  • 25
2

I encountered this same problem. It turned out WCF couldn't return DateTime as JSON, so I had to make it Nullable<DateTime>.

Robotronx
  • 1,728
  • 2
  • 21
  • 43
1

I recently had this problem, and it turned out i have forgotten to mark one of the data transfer classes with [DataContract]

ni luka
  • 61
  • 1
  • 2
1

I also had this problem and my solution was similar to Batgar's, but with a twist. I had a class that had a property of type object. I had to add KnownType attributes to the class for every type the object could hold. I couldn't populate the KnownType on-the-fly as the class didn't know what the object will contain.

Lauri Peltonen
  • 1,432
  • 15
  • 29
0

I ran into the exact same symptoms as OP, but my target was an Oracle webservice that I have no control over. WebService calls worked fine from a standalone test web-application, but not from the .NET app that required the implementation.

I don't understand why, but updating the targetFramework in web.config from 4.5 to 4.6.1 fixed the issue for my application.

<httpRuntime targetFramework="4.5" requestValidationMode="4.5" executionTimeout="36000" />

<httpRuntime targetFramework="4.6.1" requestValidationMode="4.5" executionTimeout="36000" />

kmdsax
  • 1,361
  • 9
  • 12