views:

4710

answers:

7

I have a WCF service from which I want to return a DataTable. I know that this is often a highly debated topic, as far as whether or not returning DataTables is a good practice. Let's put that aside for a moment.

When I create a DataTable from scratch, as below, there are no problems whatsoever. The table is created, populated, and returned to the client, and all is well:

[DataContract]
public DataTable GetTbl()
{
    DataTable tbl = new DataTable("testTbl");
    for(int i=0;i<100;i++)
    {
        tbl.Columns.Add(i);
        tbl.Rows.Add(new string[]{"testValue"});
    }
    return tbl;
}

However, as soon as I go out and hit the database to create the table, as below, I get a CommunicationException "The underlying connection was closed: The connection was closed unexpectedly."

[DataContract]
public DataTable GetTbl()
{
    DataTable tbl = new DataTable("testTbl");
    //populate table with sql query
    return tbl;
}

The table is being populated correctly on the server side. It is significantly smaller than the test table that I looped through and returned, and the query is small and fast - there is no issue here with timeouts or large data transfer. The same exact functions and DataContracts/ServiceContracts/BehaviorContracts are being used.

Why would the way that the table is being populated have any bearing on the table returning successfully??

+2  A: 

The attribute you want is OperationContract (on interface) / Operation Behavior (on method)

[ServiceContract]
public interface ITableProvider 
{
    [OperationContract]
    DataTable GetTbl();
}


[OperationBehavior]
public DataTable GetTbl(){    
    DataTable tbl = new DataTable("testTbl");    
    //populate table with sql query    
    return tbl;
}

Also, in the... i think service configuration... you want to specify that errors can be sent, you might be hitting an error that is something like the message size is to big, etc. You can fix that by fudging w/ the reader quotas and such. By default wsHttpBinding has a receive size quota of like 65k, so if the serialized data table's xml is more than that, it would throw an error (and i'm 95% sure the data table is more than 65k with data in it)

You can change the settings for the reader quotas and such in the web.config / app.config or you can set it on a binding instance in code. But yeah, that's probably what your problem is, if you haven't changed it by default.

WSHttpBindingBase Members - Look at ReaderQuotas property as well as the MaxReceivedMessageSize property

Darren Kopp
+4  A: 

The best way to diagnose these kinds of WCF errors (the ones that really don't tell you much) is to enable tracing. In your web.config file, add the following:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" 
              switchValue="Information" 
              propagateActivity="true">
        <listeners>
          <add name="ServiceModelTraceListener" 
               type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
               initializeData="wcf-traces.svclog"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

You can then open the resulting file in the SvcTraceViewer.exe utility which comes in the .NET Framework SDK (or with Visual Studio). On my machine, it can be found at %PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\Bin\SvcTraceViewer.exe.

Just look for an error message (in bold red) and that will tell you specifically what your problem is.

Chris Gillum
A: 

I think Darren is most likely correct - the default values provided for WCF are laughably small and if you bump into them you end up with errors that can be difficult to track down. They seem to appear as soon as you attempt to do anything beyond a simple test case. I wasted more time than I'd like to admit debugging problems that turned out to be related to the various configuration (size) settings on both the client and server. I think I ended up modifying almost all of them, ex. MaxBufferPoolSize, MaxBufferSize, MaxConnections, MaxReceivedMessageSize, etc.

Having said that, the SvcTraceViewer utility also mentioned is great. I did run into a few cases where it wasn't as helpful as I would have liked, but overall it's a nice tool for analyzing the communications flow and errors.

Paul Mrozowski
+4  A: 

For anyone having similar problems, I have solved my issue. It was several-fold.

  • As Darren suggested and Paul backed up, the Max..Size properties in the configuration needed to be enlarged. The SvcTraceViewer utility helped in determining this, but it still does not always give the most helpful error messages.
  • It also appears that when the Service Reference is updated on the client side, the configuration will sometimes not update properly (e.g. Changing config values on the server will not always properly update on the client. I had to go in and change the Max..Size properties multiple times on both the client and server sides in the course of my debugging)
  • My final problem seemed to be that the DataTable was not initialized with a name. I'm still trying to figure out why this matters, but this:

    return new DataTable();
    

    will fail, where this:

    return new DataTable("someName");
    

    will not. Any input on this would be great.

Hopefully that will help someone.

theprise
Very strange with the name thingy. I had same problem. Setting a name on the DataTable solved it... Weird.
MartinHN
+1 because setting a table name also fixed it for me.
Dave
+1 fixed for me, after I named the columns
wonea
+1  A: 

You probably blew your quota - the datatable is larger than the allowed maximum packet size for your connection.

You probably need to set MaxReceivedMessageSize and MaxBufferSize to higher values on your connection.

Sam
A: 

I'vew been trying like forever to get this donw (returning a DatatTable from a WCF service hosted in a Windows Service. Thanks guys, you've been very helpful!!!!

A: 

Thanks a ton Paul it worked wonders, I was facing same issue and was able to solvethe issue quickly

Raj