简体   繁体   English

使用WCF的IQueryable问题

[英]IQueryable problems using WCF

I have a quite simple WCF service method which returns an IQueryable, just for testing. 我有一个非常简单的WCF服务方法,它返回一个IQueryable,仅用于测试。 Perhaps I got something wrong when trying to understand what IQueryable is designed for. 也许我在试图了解IQueryable的设计时遇到了什么问题。 I clearly plan to use this with the IQueryable provider of NHibernate later. 我明确计划稍后将其与NHibernate的IQueryable提供程序一起使用。 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. 但是,每当使用返回IQueryable的WCF方法时,我首先遇到某种序列化问题(至少我认为这可能是问题)。 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. 它可能没什么意义,只是为了测试我是否真的使用WCF通过线路获得那些IQueryables。 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. 每当我使用类似SoapUI的客户端调用此方法时,我会收到套接字异常和连接重置,就像我尝试返回未标记为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: 我使用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>

The OperationContract attribute is set for the interface: 为接口设置OperationContract属性:

[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. 无论如何,我想稍后使用LINQ从IQueryable功能中获利。

Anybody any idea what's going wrong here? 有人知道这里出了什么问题吗?

Thanks and Cheers, 谢谢和干杯,

Stefan 斯特凡

The core WCF is designed to send data , not queries. 核心WCF旨在发送数据 ,而不是查询。 Stick to returning List<Foo> etc; 坚持返回List<Foo>等; 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. 但是,使用WCF数据服务 IQueryable<>更多运气,这可以让您公开IQueryable<>源。

The way this works is that the tooling builds a client that exposes similar looking IQueryable<> hooks; 这种方法的工作方式是工具构建一个客户端,公开类似的 IQueryable<>钩子; 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. 但它仍然是通过电线的结果 (而不是查询)。

(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) (已过时) AFAIK开箱即可序列化IQueryable <>或表达式树(想想它 - 这意味着需要序列化表达式树/ lambda并重建函数)

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 但是,如果有遗嘱,似乎有办法 - 你可能想看一下像这样的项目http://code.msdn.microsoft.com/exprserialization

Edit : Note that times have changed - See WCF RIA Services as per Marc Gravell's post. 编辑 :请注意,时间已经改变 - 请参阅Marc Gravell的帖子中的WCF RIA服务。

Good luck! 祝好运!

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM