【问题标题】:Need expert advice for WCF Service programming需要 WCF 服务编程的专家建议
【发布时间】:2013-05-14 17:55:24
【问题描述】:

!--- 如果这篇文章与其他一些帖子是多余的,我很抱歉,但我的项目的问题似乎是我的架构造成的,所以我需要一般帮助。 ---!

我正在尝试配置连接到 Silverlight 5 客户端的 WCF 服务和对数据库执行 CRUD 请求的 C# 类库。该服务将必须传递大量数据。例如,我的服务类中有一个 public IList GetAllRain() 方法,可以检索数据库中的数十万条记录,可能有一天会更多。所以我想我可以通过小批量获得所有这些记录(我认为这是一个很好的方法)。

这是我的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>

<!--This file contains the web configuration used by the WCF service. -->

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" maxBatchGeneratedFileSize="2147483647" maxBatchSize="2147483647" />
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>

  <system.serviceModel>

   <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
     <serviceActivations>
       <add service="PoseidonServiceNamespace.PoseidonService" relativeAddress="~/PoseidonService.svc"/>
     </serviceActivations>
   </serviceHostingEnvironment>

    <bindings>
      <basicHttpBinding>
        <binding  closeTimeout="00:01:00"
                  openTimeout="00:01:00"
                  receiveTimeout="00:10:00"
                  sendTimeout="00:01:00"
                  allowCookies="false"
                  bypassProxyOnLocal="false"
                  hostNameComparisonMode="StrongWildcard"
                  maxBufferSize="2147483647"
                  maxBufferPoolSize="2147483647"
                  maxReceivedMessageSize="2147483647"
                  messageEncoding="Text"
                  textEncoding="utf-8"
                  transferMode="Buffered"
                  useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport  clientCredentialType="None"
                        proxyCredentialType="None"
                        realm="" />
            <message clientCredentialType="UserName"
                     algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="PoseidonServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>



    <services>
      <service  behaviorConfiguration="PoseidonServiceBehavior"
                name="PoseidonServiceNamespace.PoseidonService">
        <endpoint address="http://localhost:49455/PoseidonService.svc"
                  binding="basicHttpBinding"
                  contract="PoseidonServiceNamespace.IPoseidonService"/>

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

我应该在这个文件中做哪些更改(为什么?),我应该如何在我的服务中实现我的公共 IList GetAllRain() 方法?

非常感谢, 我一直在努力解决这个问题

【问题讨论】:

    标签: .net wcf web-services silverlight web-config


    【解决方案1】:

    您确定需要将数十万条记录传输到 客户端应用程序?客户将如何查看这些记录? 这些记录会以某种方式进行预处理/汇总,还是只是 显示在某个网格中? 如果预处理 - 考虑在服务器端进行预处理。 如果只是显示 - 考虑重新设计您的界面以包含分页:

    IList<RainRecord> GetList(int startPage, int pageSize);
    

    如果分页不是一个选项,但您仍希望批量检索数据 - 您可以通过将状态引入服务来实现手动批处理 (您需要知道谁请求了列表,您目前在哪个职位, 以及还有多少数据需要传输):

    public interface IService
    {
      Guid BeginGetList();
      IList<RainRecord> GetNextBatch(Guid id);
    }
    

    GetNextBatch 返回的空列表或 null 表示传输结束。 换句话说,它是相同的分页,但使用有状态服务实现。

    现在,如果绝对有必要传输数十万条记录, 并且必须一次性完成 - 考虑using streaming to transfer your data。 但是您需要更改您的合同,将 Stream 与序列化的 RainRecords 包括在内。

    另一种选择是implement duplex service,它会传递回调 发给客户端,当客户端处理完上一个批次后,可以请求下一个。 缺点是据我所知,silverlight 不支持 WSDualHttpBinding。

    附加绑定后,您将被限制为每批 2Gb,我相信这已经足够了。此外,您可能需要调整超时,this post 详细描述所有可能的超时值。

    【讨论】:

    • 我的情况是,一些电子雨量计每 5 分钟测量一次,我的客户希望看到所有这些测量值每年随时间变化的图表,这使得在我的 silverlight 图表中绑定大约 100 000 个实体控制。他们还有其他更频繁地采取措施的工具,因此将所有这些数据组合在一起对于我的 WCF 服务来说可能是一个大问题。
    • 我会调查你的流媒体解决方案,我认为你让我走上了正轨。
    • 客户可以用 10 万个实体做什么?他们可以过滤图表吗?您是否考虑过在服务器端进行所有过滤,并仅传输足够的数据以显示生成的图表图片?这将节省网络带宽,并且应用程序将更具响应性。在我看来,这是一个经典的瘦客户端解决方案。
    • 我正在使用 Telerik 控件,即使数据量如此之大,它们也可以非常快速地过滤、排序和所有这些东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多