Returning DataTable objects with WCF service

后端 未结 3 1581
感动是毒
感动是毒 2020-12-21 00:44

This is a strange problem that I am having with WCF trying to send DataTable in the response. I have the following service contract:

[ServiceContract]
public         


        
相关标签:
3条回答
  • 2020-12-21 01:00

    While I admit that sending DataSets and DataTables via services is BAD and I actually have changed it so I am not doing so, the root of the problem lied elsewhere.

    For those that absolutely HAVE to use DataTables/DataSets, the error I was getting was because I was trying to send a DbNull object. I guess it is not serializable or there is some other reason it refused to send it.

    After manually "converting" DbNull into null (I had to do this to extract data into my own custom DataContract anyways), the error was gone and it worked!

    0 讨论(0)
  • 2020-12-21 01:01

    I know this is an old question, but maybe someone is still experiencing the same problem (like I did). I was faced with the same confusing error message and spent hours banging my head when it hit me....

        public DataTable GetDataTable()
        {
            DataTable dt = new DataTable(**"MY_NAME"**);
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Caption", typeof(string));
    
            dt.Rows.Add(new object[] { 1, "hooray!" });
            return dt;
        }
    

    you should give your DataTable a name, i used a overloaded constructor and no more errors!

    EDIT: Of course, you really shouldn't use DataTable or DataSet as a return type from a WCF service. For me it was just for testing purposes, because I thought it's the quickest way to get something out of a database and over the wire....boy was i wrong :)

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

    Using DataSets and DataTables aren't best practice. They are quite bulky, best to use a collection of dataclasses (poco's / dto's).

    Some background info:

    Returning DataSets from WebServices is the Spawn of Satan and Represents All That Is Truly Evil in the World

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