有没有办法从 WCF 方法返回 DynamicClass
类型的对象数组?
我正在使用 动态 Linq 库,以便根据客户端的请求选择
数据库表的列。客户端代码应如下所示:
//client side code
string whereClause = "FeatureId >= 6 and FeatureId <= 180";
string selectClause = "New(FeatureName as Name, FeatureId as Id)";
client.RequestAsync("Feature", "FeatureDB", whereClause, selectClause);
功能是我只想从中选择
两列的表的名称,即。 FeatureName 和 FeatureId,满足 where
子句中的条件。
这里的问题是查询在服务器上运行良好,但 WCF 无法将其发送回客户端。我的猜测是,仅定义选定列的动态创建的类未声明 DataContract
,因此 WCF 无法使用它。
那么这个问题有什么解决办法吗?
或者有什么替代方案吗?目标是,我不想返回数据库表的所有列,因为我不需要客户端上的所有列。因此,我认为将所有列发送回客户端没有任何意义,客户端无论如何都会丢弃它。
Is there any way to return an array of objects of type DynamicClass
from WCF method?
I'm using Dynamic Linq Library in my WCF service, so as to select
columns of database table , as per the request from clients. The client code should look like this:
//client side code
string whereClause = "FeatureId >= 6 and FeatureId <= 180";
string selectClause = "New(FeatureName as Name, FeatureId as Id)";
client.RequestAsync("Feature", "FeatureDB", whereClause, selectClause);
Feature is the name of the table from which I want to select
two columns only, viz. FeatureName and FeatureId, satisfying the condition in the where
clause.
Here, the problem is that the query runs fine on the server, but WCF is unable to send it back to the client. My guess is that the dynamically created class which defines only the selected columns is not declared DataContract
, so WCF isn't able to work with it.
So any solution to this problem?
Or any alternative? The goal is, I don't want to return all columns of the database table, because I don't need all of them on the client side. So I don't see any point sending all columns back to the client, who will discard it anyway.
发布评论
评论(2)
您可以通过使用反射用适当的属性装饰您的字段来解决此问题,但这仍然可能会给使用 WCF 服务的任何客户端进程带来问题,因为服务合同的生成部分将是不确定的,即模式中的 xs:any。
最好尽可能尝试强类型化您的 WCF 合同。
You may be able to work around this by using Reflection to decorate your fields with the appropriate attributes, however this may still present an issue for any client process that consumes your WCF service, as the generated section of your service contract will be non-deterministic, i.e. xs:any in the schema.
Better to try and strongly type your WCF contracts wherever possible.
您是否考虑过实施WCF 数据服务?
Have you considered implementing a WCF Data Service?