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

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

问题描述

这是一个奇怪的问题,我在 WCF 尝试在响应中发送 DataTable 时遇到了这个问题.我有以下服务合同:

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!";
}

现在,当我这样离开时,总是在调用 LoadData(...) 方法时出错:

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

接收 HTTP 响应时出错http://localhost:8731/Design_Time_Addresses/DataProviderServiceLibrary/SapphireDataService/.这可能是由于服务端点绑定未使用 HTTP协议.
这也可能是由于 HTTP 请求上下文被被服务器中止(可能是由于服务关闭).
有关详细信息,请参阅服务器日志.

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.

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

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!

所以我所做的就是把我从数据库中得到的这个表放到一个 DataSet 对象中并且它起作用了!没有错误或任何类似的东西.但是,DataSet 中返回的表是 EMPTY. 所有字段都是 Null,并且没有数据被正确传输/反序列化,看起来 :(

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?

推荐答案

虽然我承认通过服务发送 DataSets 和 DataTables 是不好的并且我实际上已经改变了它所以我没有这样做,但问题的根源在于其他地方.

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.

对于那些绝对必须使用 DataTables/DataSets 的人,我得到的错误是因为我试图发送一个 DbNull 对象.我猜它是不可序列化的,或者有其他一些原因它拒绝发送它.

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.

在手动将 DbNull转换"为 null 之后(无论如何我必须这样做才能将数据提取到我自己的自定义 DataContract 中),错误消失了,它起作用了!

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天全站免登陆