使用WCF的IQueryable问题

发布于 2024-09-11 09:09:39 字数 1982 浏览 4 评论 0原文

我有一个非常简单的 WCF 服务方法,它返回一个 IQueryable,仅用于测试。也许我在尝试理解 IQueryable 的设计目的时犯了一些错误。我显然计划稍后将其与 NHibernate 的 IQueryable 提供程序一起使用。但首先,每当使用返回 IQueryable 的 WCF 方法时,我都会遇到某种序列化问题(至少我认为这可能是问题)。它甚至不适用于简单的字符串。

这是我的代码:

public IQueryable<string> GetEquipmentConfigurations()
{
  var returnValue = new List<string>();
  returnValue.Add("test");
  return returnValue.AsQueryable();
}

它可能没有多大意义,它只是为了测试我是否真的使用 WCF 通过网络获取这些 IQueryables。每当我使用像 SoapUI 这样的客户端调用此方法时,我都会收到套接字异常和连接重置,就像我试图返回未标记为 DataContract 的内容一样。但我在这里做的唯一一件事就是尝试返回一些糟糕的字符串列表。这有什么问题吗?

我使用 basicHTTPBinding,这是我的设置:

<system.serviceModel>
   <services>
      <service name="EquipmentConfigurationService" behaviorConfiguration="DefaultBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/Krones.KBase/Services/EquipmentConfigurationService"/>
          </baseAddresses>
        </host>
        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="Krones.MES.KBase.Public.Service.EquipmentDefinition.IEquipmentConfigurationService" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
   </services>
   <behaviors>
      <serviceBehaviors>
         <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="True"/>
            <serviceDebug includeExceptionDetailInFaults="True"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>
</system.serviceModel>

为接口设置了 OperationContract 属性:

[OperationContract]
IQueryable<string> GetEquipmentConfigurations();

当仅返回一个简单的字符串时,一切都运行良好。无论如何,我想稍后使用 LINQ 从 IQueryable 功能中获利。

有人知道这里出了什么问题吗?

谢谢和干杯,

斯特凡

I have a quite simple WCF service method which returns an IQueryable, just for testing. Perhaps I got something wrong when trying to understand what IQueryable is designed for. I clearly plan to use this with the IQueryable provider of NHibernate later. But first I ran into some sort of serialization problems (at least I think it might be the problem) whenever using a WCF method returning an IQueryable. It doesn't even work for a simple string.

Here's my code:

public IQueryable<string> GetEquipmentConfigurations()
{
  var returnValue = new List<string>();
  returnValue.Add("test");
  return returnValue.AsQueryable();
}

It might not have much sense, it's just for testing whether I really get those IQueryables over the wire using WCF. Whenever I call this method using a client like SoapUI I get a socket exception and a connection reset, just the same as if I was trying to return something that is not marked as DataContract. But the only thing I do here is trying to return some lousy string list. What's wrong with that?

I use basicHTTPBinding, here are my settings:

<system.serviceModel>
   <services>
      <service name="EquipmentConfigurationService" behaviorConfiguration="DefaultBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/Krones.KBase/Services/EquipmentConfigurationService"/>
          </baseAddresses>
        </host>
        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="Krones.MES.KBase.Public.Service.EquipmentDefinition.IEquipmentConfigurationService" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
   </services>
   <behaviors>
      <serviceBehaviors>
         <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="True"/>
            <serviceDebug includeExceptionDetailInFaults="True"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>
</system.serviceModel>

The OperationContract attribute is set for the interface:

[OperationContract]
IQueryable<string> GetEquipmentConfigurations();

It all works well when just returning a simple string. Anyway I want to take profit from the IQueryable features using LINQ later.

Anybody any idea what's going wrong here?

Thanks and Cheers,

Stefan

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

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

发布评论

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

评论(2

葵雨 2024-09-18 09:09:39

核心 WCF 旨在发送数据,而不是查询。坚持返回 List 等;它会让你省去很多麻烦。

不过,使用 WCF 数据服务,它允许您公开 IQueryable 源。

其工作原理是该工具构建一个客户端,该客户端公开类似的外观 IQueryable 挂钩;当您查询数据时,它代表线路上的表达式,查询数据并将其返回给客户端。但通过网络传输的仍然是结果(而不是查询)。

The core WCF is designed to send data, not queries. Stick to returning List<Foo> etc; it'll save you much head-scratching.

However, you might have more luck doing what you are after with WCF Data Services, which allows you to expose IQueryable<> sources.

The way this works is that the tooling builds a client that exposes similar looking IQueryable<> hooks; when you query data, it represents the expression on the wire, queries the data and brings it back to the client. But it is still the results (not the query) that goes over the wire.

找回味觉 2024-09-18 09:09:39

(已过时)
AFAIK 不可能开箱即用地序列化 IQueryable<>或表达式树(想一想 - 这意味着表达式树/lambda 需要序列化并重建函数)

但是,只要有意愿,似乎就有办法 - 您可能想看看这样的项目作为这个
http://code.msdn.microsoft.com/exprserialization

编辑 :请注意,时代已经改变 - 请参阅 Marc Gravell 的帖子中的 WCF RIA 服务。

祝你好运!

华泰

(Obsolete)
AFAIK it isn't possible out of the box to serialize IQueryable<> or Expression Trees (think about it - it would mean that the expression tree / lambda would need to be serialized and the function rebuilt)

However, where there is a will, it seems there is a way - you might want to look at projects such as this
http://code.msdn.microsoft.com/exprserialization

Edit : Note that times have changed - See WCF RIA Services as per Marc Gravell's post.

Good luck!

HTH

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