Returning DataTables in WCF/.NET

前端 未结 8 1576
广开言路
广开言路 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:51

    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)
    • For a DataTable to be serializable, it needs to be given a name. The default constructor does not give the table a name, so:

      return new DataTable();
      

      will not be serializable, while:

      return new DataTable("someName");
      

      will name the table whatever is passed as the parameter.

      Note that a table can be given a name at any time by assigning a string to the TableName property of the DataTable.

      var table = new DataTable();
      table.TableName = "someName";
      

    Hopefully that will help someone.

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

    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 with the reader quotas and such.

    By default wsHttpBinding has a receive size quota of like 65 KB, 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 65 KB 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.

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