Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning DataTable objects with WCF service

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 interface ISapphireDataService {
    [OperationContract]
    DataTable LoadData(string query, DateTime start, DateTime end);

    [OperationContract]
    string LoadDataTest();
}

And the following implementation of the methods (where provider is a class that makes the database call and returns back a DataTable):

public DataTable LoadData(string query, DateTime start, DateTime end) {
    //DataSet temp = new DataSet();
    //temp.Tables.Add(provider.LoadData(query, start, end).Copy());
    //return temp;

    return provider.LoadData(query, start, end).Copy();
}

public string LoadDataTest() {
    return "Hello World!";
}

Now, when I leave it like this, I always get an error when calling the LoadData(...) method:

An error occurred while receiving the HTTP response to http://localhost:8731/Design_Time_Addresses/DataProviderServiceLibrary/SapphireDataService/. This could be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).
See server logs for more details.

This is rather odd because the service is configured to use the wsHttpBinding, which I assume uses HTTP propocol. This error does not occur if I try calling the LoadDataTest() method!

So what I have done was put this table I have gotten from the database into a DataSet object and it worked! No errors or anything of the sort. BUT, the table that was returned in the DataSet was EMPTY. All the fields were Null and no data has been transfered/de-serialized properly, it seems :(

This seems to be a common issue but I am yet to see an answer to this that works. Any ideas?

like image 862
Alexandra Avatar asked Sep 04 '09 17:09

Alexandra


1 Answers

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 :)

like image 133
OttO Avatar answered Oct 12 '22 05:10

OttO