C# WCF:从多行返回单行

C# WCF:从多行返回单行,c#,asp.net,.net,wcf,C#,Asp.net,.net,Wcf,我正在尝试从WCF服务获取数据并将其显示在网站上。 通信如下所示: 网站-->WCF服务-->CRM服务器-->WCF服务-->网站 现在我的问题是,有时需要获取更大的数据,大约5k行。(我的主机内存不足) 我想流1-10行到网站,然后下一个,依此类推 我的服务合同如下所示: public interface ICommunicationService { [OperationContract] IEnumerable<Row> GetCrmData(stri

我正在尝试从WCF服务获取数据并将其显示在网站上。
通信如下所示:
网站-->WCF服务-->CRM服务器-->WCF服务-->网站

现在我的问题是,有时需要获取更大的数据,大约5k行。(我的主机内存不足) 我想流1-10行到网站,然后下一个,依此类推

我的服务合同如下所示:

public interface ICommunicationService
{    
    [OperationContract]
    IEnumerable<Row> GetCrmData(string view);

}
1.收益率回报是否正确使用

绑定
WCF服务:


WCF客户端

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICommunicationService" 
                 closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" 
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                 maxBufferSize="65536" 
                 maxReceivedMessageSize="4294967294" 
                 messageEncoding="Text" 
                 textEncoding="utf-8" 
                 transferMode="Streamed" 
                 useDefaultWebProxy="true">
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_ICommunicationService" 
                contract="ComToCrmReference.ICommunicationService" 
                name="BasicHttpBinding_ICommunicationService"
                address="http://dev11.meta10.com:9007/WCFTestService/CommunicationService.svc" />
    </client>
  </system.serviceModel>

2.绑定是否正确

3.我的思维有问题吗? 请记住,我正在尝试从5000行中获取1-10行,然后在网站上显示它们,获取下一个1-10行,依此类推。
只有数据,没有二进制数据或类似的东西


4.仅仅一个请求就可以吗?

首先,请记住,WCF生活在面向服务的范式中,而不是面向对象的范式中。对WCF的许多误解在于它能够包装服务调用并以面向对象的方式呈现服务调用

您能够从您的服务返回
IEnumerable
就是一个例子。我们被愚弄了,以为我们是在按照抽象进行编程,而实际上WCF必须通过连接(非常具体的实现)序列化服务调用的结果,然后将其转换到客户端上的接口

因此,您无法对所需的序列执行“惰性”评估—是的—您将在整个过程中拉动整个结果集,即使您只需要前10个结果集

WCF确实支持流式传输(),但更简单的方法可能是在服务合同中添加参数,以指示您需要的页面信息:

public interface ICommunicationService
{
    [OperationContract]
    Row[] GetCrmData(string view, int pageNumber, int pageSize);
}
该契约更符合无状态面向服务的应用程序,尊重这样一个概念,即您不应该在系统中传递大量数据,而应该只使用您需要的数据


请注意,我已经用数组替换了
IEnumerable
,消除了您正在通过有线接口编程的印象。

如果我是对的,您建议只加载10行(如果我当时只显示10行),然后在加载站点时加载更多页面?最简单的形式是,尽管您可以很聪明地加载前5页,比如在启动时,或者根据应用程序的使用模式定制一些其他缓存算法。
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="ServiceBehavior" 
           name="ComToCrmService.CommunicationService">
    <endpoint binding="basicHttpBinding" 
              bindingNamespace="http://localhost:9006/CommunicationService.svc" 
              contract="ComToCrmContracts.ICommunicationService" />
  </service>
</services>
<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICommunicationService" 
                 closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" 
                 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                 maxBufferSize="65536" 
                 maxReceivedMessageSize="4294967294" 
                 messageEncoding="Text" 
                 textEncoding="utf-8" 
                 transferMode="Streamed" 
                 useDefaultWebProxy="true">
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_ICommunicationService" 
                contract="ComToCrmReference.ICommunicationService" 
                name="BasicHttpBinding_ICommunicationService"
                address="http://dev11.meta10.com:9007/WCFTestService/CommunicationService.svc" />
    </client>
  </system.serviceModel>
public interface ICommunicationService
{
    [OperationContract]
    Row[] GetCrmData(string view, int pageNumber, int pageSize);
}