Returning DataTables in WCF/.NET

前端 未结 8 1575
广开言路
广开言路 2020-12-01 05:00

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 pract

相关标签:
8条回答
  • 2020-12-01 05:32

    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.

    0 讨论(0)
  • 2020-12-01 05:36

    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.

    0 讨论(0)
  • 2020-12-01 05:41

    I added the Datable to a data set and returned the table like so...

    DataTable result = new DataTable("result");
    
    //linq to populate the table
    
    Dataset ds = new DataSet();
    ds.Tables.Add(result);
    return ds.Tables[0];
    

    Hope it helps :)

    0 讨论(0)
  • 2020-12-01 05:45

    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.

    0 讨论(0)
  • 2020-12-01 05:48

    There are 3 reason for failed return type as datatable in WCF services

    • You have to specify data table name like:

      MyTable=new DataTable("tableName");
      
    • When you are adding reference on client side of WCF service select reusable dll system.data

    • Specify attribute on datatable member variable like

      [DataMember]
      public DataTable MyTable{ get; set; }
      
    0 讨论(0)
  • 2020-12-01 05:49

    Other than setting maximum values for all binding attributes.

    Make sure each table you are passing/returning from webservice must have a table name, meaning the table.tablename property should not be blank.

    0 讨论(0)
提交回复
热议问题