如何在WCF服务中使用数据表代码没有运行有错误 [英] How to use datatable in WCF services the code is not running having error

查看:80
本文介绍了如何在WCF服务中使用数据表代码没有运行有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

wcf测试客户端不支持此操作,因为它使用类型System.data.datatable





svc文件代码



public System.Data.DataTable PendingCustomerViewAll()

{

DataTable tbl = new DataTable();

试试

{



PendingBillSP sp = new PendingBillSP();

tbl = sp.PendingCustomerViewAll();

// DataSet ds = new DataSet();

//ds.Tables.Add(tbl),





}

catch(例外)

{

}

返回tbl;



}





[OperationContract]



DataTable PendingCustomerViewAll();



我的尝试:



如何使用DataTable在wcf服务中,代码运行时没有错误

this operation is not supported in wcf test client because it uses type System.data.datatable


svc file code

public System.Data.DataTable PendingCustomerViewAll()
{
DataTable tbl = new DataTable();
try
{

PendingBillSP sp = new PendingBillSP();
tbl = sp.PendingCustomerViewAll();
//DataSet ds = new DataSet();
//ds.Tables.Add(tbl);


}
catch (Exception)
{
}
return tbl;

}


[OperationContract]

DataTable PendingCustomerViewAll();

What I have tried:

how to use DataTable In wcf services the code is not running having error

推荐答案

返回DataTables(或DataSet) )出于各种原因,来自WCF-Services通常不是一个好主意。最突出的两个是:

1) .NET以外的客户无法处理它。

2)默认的序列化性能非常糟糕。

进一步阅读: c# - 在WCF / .NET中返回DataTables - 堆栈溢出 [ ^ ]



一般情况下它确实有效,但说实话,我不知道你的问题可能是什么原因。


但是:如果您对客户必须是.NET没问题,那么解决第二个问题(序列化性能)也将解决您当前的问题。



我正在使用这个库来序列化DataTables: GitHub - rdingwall / protobuf-net-data:用于序列化ADO的库将.NET DataTables和DataReader转换为可移植的二进制格式。 [ ^ ]



有了它,您可以独立于WCF将DataTable序列化为字节数组(并反序列化)。然后,您的WCF方法的返回类型将是 byte [] 而不是 DataTable 并且WCF没有问题。此外,序列化性能优于默认值,包括速度和大小。



如果你想让它比使用<$更漂亮c $ c> byte [] 作为返回类型,您可以创建一个自定义类,它保存序列化的DataTable并使该类成为WCF方法的返回类型。例如,如果您将其命名为SerializedDataTable,则类型的名称更好地描述返回值的语义内容,而不是 byte [] 。您还可以将DataTable的序列化和反序列化所需的代码放入该类中,以便很好地封装它。
Returning DataTables (or DataSets) from WCF-Services is typically not a very good idea, for various reasons. The two most prominent ones being:
1) Clients other than .NET can't deal with it.
2) The default serialization performance is really bad.
Further reading: c# - Returning DataTables in WCF/.NET - Stack Overflow[^]

In general it does work though and, to be honest, I don't know from the top of my head what could be the cause of your problem.

But: If you're okay with clients having to be .NET then solving the second issue (serialization performance) will also solve your current problem here.

I'm using this library for serializing DataTables: GitHub - rdingwall/protobuf-net-data: A library for serializing ADO.NET DataTables and DataReaders into a portable binary format.[^]

With it you can serialize a DataTable into a byte-array (and deserialize it) independently from WCF. The return type of your WCF-method then would be byte[] instead of DataTable and WCF has no problem with that. Also the serialization performance is way better than the default one, both in speed as well as in size.

If you want to make it a bit prettier than using byte[] as the return type then you could create a custom class which holds the serialized DataTable and make that class the return type of your WCF-method. Benefit being that the name of the class type better describes the semantic content of the return value than byte[] if you name it "SerializedDataTable", for example. You could also put the required code for serialization and deserialization of the DataTable into that class so that it's nicely encapsulated.


这篇关于如何在WCF服务中使用数据表代码没有运行有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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