23

please somebody can help me to find out what is happened. I have my WCF service which worked fine, and now suddenly I have this error:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error

I must tell that it still works when I select some thousands of records, but when the data is huge I receive this error, although before it worked fine!

    private static string ConnString = "Server=127.0.0.1; Port=5432; Database=DBname; User Id=UName; Password=MyPassword;"
    DataTable myDT = new DataTable();

                NpgsqlConnection myAccessConn = new NpgsqlConnection(ConnString);
                myAccessConn.Open();
        string query = "SELECT * FROM Twitter";

                NpgsqlDataAdapter myDataAdapter = new NpgsqlDataAdapter(query, myAccessConn);

                myDataAdapter.Fill(myDT);
                foreach (DataRow dr in myDT.Rows)
                {
   **WHEN I HAVE TOO MANY RECORDS IT STOPS HERE**
        ...

web.config

<configuration>
    <system.web>
        <compilation debug="false" targetFramework="4.0" />
      <httpRuntime maxRequestLength="2147483647" executionTimeout="100000" />
    </system.web>
  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="Traces4.svclog"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
  <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IDBService" closeTimeout="00:30:00"
                    openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
    <client>
      <endpoint address="" binding="basicHttpBinding" 
          bindingConfiguration="BasicHttpBinding_IDBService" contract="DBServiceReference.IDBService"
          name="BasicHttpBinding_IDBService" />
    </client>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                  <serviceMetadata httpGetEnabled="true" />
                  <serviceDebug includeExceptionDetailInFaults="true" />
                  <dataContractSerializer maxItemsInObjectGraph="2147483646" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
</configuration>

client config (Edited)

<configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IRouteService" maxBufferSize="2147483647"
                        maxReceivedMessageSize="2147483647">
                        <security mode="None" />
                    </binding>
                    <binding name="BasicHttpBinding_IDBService" closeTimeout="00:30:00"
                        openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
                        maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
                        transferMode="Buffered" >

                        <security mode="None" />
                    </binding>
                </basicHttpBinding>
                <customBinding>
                    <binding name="CustomBinding_IRouteService">
                        <binaryMessageEncoding />
                        <httpTransport maxReceivedMessageSize="2147483647"
                            maxBufferSize="2147483647" />
                    </binding>
                </customBinding>
            </bindings>

            <client>
                <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRouteService"
                    contract="BingRoutingService.IRouteService" name="BasicHttpBinding_IRouteService" />
                <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc/binaryHttp"
                    binding="customBinding" bindingConfiguration="CustomBinding_IRouteService"
                    contract="BingRoutingService.IRouteService" name="CustomBinding_IRouteService" />
                <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDBService"
                    contract="DBServiceReference.IDBService" name="BasicHttpBinding_IDBService" />
            </client>
        </system.serviceModel>
    </configuration>

In my file scvlog I don' t get any exception! I don't have any other idea what else I can do for understand where is the problem. Please somebody help me!!!

Aliya
  • 313
  • 1
  • 2
  • 9
  • 1
    Is this the complete web.config...?! I can't see a `system.serviceModel/services/service`-section. – Jan Köhler Apr 23 '13 at 17:25
  • Yea I need to see more of the config. However, since there are so may records you may be having a timeout issue – Jeff Apr 23 '13 at 18:27
  • Sorry for mistake, I have edited the client config. @Jeff, The strange thing is that It worked before, I didn’t have any problems with timeout or anything else. – Aliya Apr 24 '13 at 06:07
  • You edited the client config. I meant the web.config. It's at least missing the declaration of a service... – Jan Köhler Apr 25 '13 at 07:36
  • @justMe, yes, that's all what I have in web.config. I define endpoint configuration name and address in code: System.ServiceModel.EndpointAddress DBServiceEndpointAddress = new System.ServiceModel.EndpointAddress(new Uri(Application.Current.Host.Source, "../DBService.svc")); DBServiceReference.DBServiceClient DBClient = new DBServiceReference.DBServiceClient("BasicHttpBinding_IDBService", DBServiceEndpointAddress); – Aliya Apr 28 '13 at 16:46

10 Answers10

22

A different answer, just in case anyone arrives here as I did looking for a general answer to the question.

It seems that the DataContractSerializer that does the donkey-work is incredibly finicky, but doesn't always pass the real error to the client. The server process dies straight after the failure - hence no error can be found. In my case the problem was an enum that was used as flags, but not decorated with the [Flags] attribute (picky or what!).

To solve it I created an instance of the serializer and inspected the error in the debugger; here's a code snippet since I have it to hand.

EDIT: In response to request in comments ...

Amended the code snippet to show the helper method I now use. Much the same as before, but in a handy generic wrapper.

public static T CheckCanSerialize<T>(this T returnValue) {
    var lDCS = new System.Runtime.Serialization.DataContractSerializer(typeof(T));

    Byte[] lBytes;
    using (var lMem1 = new IO.MemoryStream()) {
        lDCS.WriteObject(lMem1, returnValue);
        lBytes = lMem1.ToArray();
    }

    T lResult;
    using (var lMem2 = new IO.MemoryStream(lBytes)) {
        lResult = (T)lDCS.ReadObject(lMem2);
    }

    return lResult;
}

And to use this, instead of returning an object, return the object after calling the helper method, so

public MyDodgyObject MyService() {
    ... do lots of work ...
    return myResult;
}

becomes

public MyDodgyObject MyService() {
    ... do lots of work ...
    return CheckCanSerialize(myResult);
}

Any errors in serialization are then thrown before the service stops paying attention, and so can be analysed in the debugger.

Note; I wouldn't recommend leaving the call in production code, it has the overhead of serializing and deserializing the object, without any real benefit once the code is debugged.

Hope this helps someone - I've wasted about 3 hours trying to track it down.

Richard Petheram
  • 805
  • 12
  • 16
  • 3
    I really want to try out your suggestion to get the real error message, but it's not clear where this should be implemented. – friggle Aug 27 '14 at 15:15
  • It should be the last thing the service does before sending the results back to the client. Most errors can be trapped in the service and dealt with in a straightforward manner, but it's not so easy to catch problems with converting the results of the method call as they leave the service; this technique mimics that final serialization so that errors can be seen before the service disappears. – Richard Petheram Aug 27 '14 at 21:48
  • 2
    YOU ARE AMAZING!! This solved my problem and unfortunately I was stuck on this for a couple days.... If I could hug you I would, but thank you so much!! – Jose Oct 28 '15 at 17:57
  • Just like @friggle and others I couldn't understand exactly how I could apply your solution. Could you please give a more complete example, possibly with code which highlights both the calling client code and also the server WCF code (which would be really great for replicating the solution, I think). – Ulysses Alves Feb 14 '18 at 16:55
  • @UlyssesAlves I've amended the code. There's nothing to do in the calling client code, this is solely in the server. Put your return object as the parameter of CheckCanSerialize and it will serialize and then deserialize it. If there are any problems with the process you should be able to see it in the debugger before the service call gets cleaned up. – Richard Petheram Feb 15 '18 at 16:31
  • I thought about editing your answer, then decided it would be better to just add a comment instead. I got this to work with very few edits, thank you for the excellent answer! I get the answer I needed the first time I ran it - was using a DataMemberAttribute for an enum member, needed an EnumMemberAttribute. – DaveN59 Nov 15 '19 at 18:59
  • Anyway, the edits - you implemented this as an extension method, so the correct syntax should be return myResult.CheckCanSerialize(); No big deal, but that is the correct way to call an extension method. Edit if you feel like it... :) – DaveN59 Nov 15 '19 at 19:01
8

I don't know if it's really can be an answer, but I have tried to change in web.config from <security mode="None" /> to <security mode="Transport" /> and It worked!!!

I'd want to pay attention that this part should be changed only in web.config and in client configuration remains <security mode="None" />, because with Transport in both It doesn't work!

So after that, I decided to try to come back again to None security and It worked for some minutes and then stopped again, and it came back the error:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error

So It seems that the solution in my case is to set in web.config

security mode to Transport

Community
  • 1
  • 1
Aliya
  • 313
  • 1
  • 2
  • 9
  • Unfortunately I'm facing again the same error, I can't understand what is wrong and why sometimes my WCF service works and in other times I get this error. Please, if anyone has any idea, write me! – Aliya Apr 29 '13 at 06:20
  • I have tried to use fiddler, and I noticed that the response body is always "124.300.487 bytes" when there is an error and when the service works without returning an error. Is it possible that it crashes on client side? And another thing, during the testing I have had also a couple of times "System.InsufficientMemoryException Failed to allocate a managed memory buffer of 233702096 bytes. The amount of available memory may be low." – Aliya Apr 29 '13 at 09:02
  • Have you get to a definite solution for this problem? – Ulysses Alves Feb 14 '18 at 17:05
5

In my case, I was working on a windows app project communicating with a WCF Web Service. The web service, using netTcpBinding was returning a Stream object (a picture).

As the windows app doesn't have configuration file, default values are used for bindings. And simply extending the MaxReceivedMessageSize on the client side backend code solved my problem.

var API = new StreamService.StreamServiceClient(
  new System.ServiceModel.NetTcpBinding(System.ServiceModel.SecurityMode.None)
  {
    MaxReceivedMessageSize = 2147483647
  },
  new System.ServiceModel.EndpointAddress("net.tcp://machine/app/service.svc")
);
Aurel
  • 405
  • 5
  • 8
3

Sometimes this problem is caused by an oversized message that was cut due to default values in the binding.

You should add maxReceivedMessageSize, maxBufferPoolSize and maxBufferSize with some large enough values to the binding in your app.config file - that should do the trick :)

Example:

<bindings>
<netTcpBinding>
<binding 
name="ExampleBinding" closeTimeout="00:01:00"
maxReceivedMessageSize="73400320"
maxBufferPoolSize="70000000"
maxBufferSize="70000000"/>
</netTcpBinding>
</bindings>

Good Luck!

Eking
  • 758
  • 8
  • 16
2

In my case I was working on an MVC application and I have changed

maxReceivedMessageSize ="10000000"

to

maxReceivedMessageSize ="70000000"

and it worked! It's because the response from the web server exceeds maxReceivedMessageSize ="10000000",
so I have increased maxReceivedMessageSize to maxReceivedMessageSize ="70000000".

zx485
  • 28,498
  • 28
  • 50
  • 59
Rajesh M
  • 21
  • 1
1

In my experience of this error, just check the service's host computer's event log to see what is the actual root exception.

Kev
  • 1,832
  • 1
  • 19
  • 24
  • This is actually the best way to debug your issue. In my case, I had the issue for WCF logged in the Event Logs as such: Exception: System.ServiceModel.ServiceActivationException: The service '/INT.ComplianceService/ComplianceService.svc' cannot be activated due to an exception during compilation. The exception message is: Memory gates checking failed because the free memory (223719424 bytes) is less than 5% of total memory. As a result, the service will not be available for incoming requests. – Alain Ghawi Aug 11 '21 at 17:27
0

For me it was a lazy-loading list of items retrieved from the DB.

The WCF receiver would try to iterate them, which would try to go to the DB, which obviously could not work.

ANeves
  • 6,219
  • 3
  • 39
  • 63
  • 1
    I probably took the Enumerable and did `.ToArray()` or `.ToList()` (in the data access layer), to persist it and avoid the later lazy-loading. – ANeves Apr 27 '15 at 12:52
0

In BizTalk we use to get this issue.

Mostly the issue will happen due to size of the message from the service. So we need to increase the size of the receiving message from 65,356 to 2,365,60. It worked for me.

enter image description here

0

ASP.NET applications can execute with the Windows identity (user account) of the user making the request. Impersonation is commonly used in applications that rely on Microsoft Internet Information Services (IIS) to authenticate the user. ASP.NET impersonation is disabled by default.

Enable this, your API will start working - it is in IIS authentication

0

In my case, after upgrading from .NET Framework 4.5 to .NET Framework 4.8, I had to remove read-only modifiers of properties that were decorated with DataMemberAttribute.

Fung
  • 7,530
  • 7
  • 53
  • 68