在 WCF 中动态创建类作为 DataContract

发布于 2024-11-14 14:27:34 字数 874 浏览 3 评论 0 原文

有没有办法从 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);

功能是我只想从中选择两列的表的名称,即。 FeatureNameFeatureId,满足 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-11-21 14:27:34

您可以通过使用反射用适当的属性装饰您的字段来解决此问题,但这仍然可能会给使用 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.

萌吟 2024-11-21 14:27:34

您是否考虑过实施WCF 数据服务

Have you considered implementing a WCF Data Service?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文