使用WCF服务返回DataTable对象 [英] Returning DataTable objects with WCF service

查看:93
本文介绍了使用WCF服务返回DataTable对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的问题,我正在用WCF试图在响应中发送DataTable。我有以下服务合同:

  [ServiceContract] 
public interface ISapphireDataService {
[OperationContract]
DataTable LoadData(string query,DateTime start,DateTime end);

[OperationContract]
string LoadDataTest();
}

以下方法的实现(provider是一个使数据库调用并返回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(){
returnHello World!;
}

现在,当我这样离开时,我总是在打电话时收到错误LoadData(...)方法:


收到HTTP响应
时发生错误 http:// localhost:8731 / Design_Time_Addresses / DataProviderServiceLibrary / SapphireDataService /
这可能是由于服务端点绑定不使用HTTP
协议。

这也可能是由于服务器中断
的HTTP请求上下文(可能是由于服务关闭)。

有关详细信息,请参阅服务器日志。


这很奇怪因为服务被配置为使用wsHttpBinding,我假设使用HTTP propocol。如果我尝试调用LoadDataTest()方法,这个错误不会发生!



所以我做的是把这个表从数据库变成一个DataSet对象,它工作!没有错误或任何类型的。 但是,DataSet中返回的表格为EMPTY。所有字段都为空,并且没有正确地传输/解除序列化的数据,似乎(



这似乎是一个常见的问题,但我还没有看到一个有用的答案。任何想法?

解决方案

虽然我承认通过服务发送数据表和数据表是BAD,我实际上已经改变了,所以我没有这样做,但是问题的根源在于其他地方。



对于那些绝对需要使用DataTables / DataSet的人,我所得到的错误是因为我正在尝试发送一个DbNull对象,我猜这是不可序列化的,还有一些其他原因拒绝发送它。 >

手动转换DbNull为null(我不得不这样做,将数据提取到我自己的自定义DataContract中),错误已经消失,它的工作!


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?

解决方案

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!

这篇关于使用WCF服务返回DataTable对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆