使用MS Sync框架和iis托管的WCF服务同步2mb条目的表需要10分钟

本文关键字:2mb 同步 10分钟 服务 WCF 框架 Sync MS iis 使用 | 更新日期: 2023-09-27 18:17:06

我在一台服务器上运行iis托管的WCF服务,在另一台服务器上运行windows服务。windows服务作为从属服务器,通过连接到iis托管的WCF服务来同步每个服务器上的数据库,使用MS Sync框架。

我将文件的内容存储在一个正在同步的表中,条目可能会变得有点大(每个条目约10mb)。我的问题是,当我尝试同步这些条目时,需要很长时间才能同步。

正在被同步的表:

[Id] [int] IDENTITY(1,1) NOT NULL,
[Client] [uniqueidentifier] NOT NULL,
[Date] [datetime] NOT NULL,
[Content] [nvarchar](max) NOT NULL,

我已经将1个具有不同内容大小的条目的同步时间设置为:

  • 500kb - 1min 20sec
  • 1mb - 3min
  • 1.5 mb - 6min 30秒
  • 2mb - 9min 50sec

(在Visual Studio中使用本地服务和数据库进行调试时,时间大致相同)

有人能解释一下为什么要花这么长时间吗?

WCF服务配置:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2147483647" />
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding closeTimeout="00:20:00" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20: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>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Windows service config:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ISyncServiceContract" closeTimeout="00:20:00" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20: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>
    <client>
      <endpoint address="http://localhost:60000/SyncService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISyncServiceContract" contract="SyncServiceReference.ISyncServiceContract" name="BasicHttpBinding_ISyncServiceContract"/>
    </client>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

使用MS Sync框架和iis托管的WCF服务同步2mb条目的表需要10分钟

似乎问题是nvarchar(max)字段时,它包含太多的数据。我将字段更改为varbinary(max),现在相同数据的同步在之前使用的时间的一小部分内完成。

如果您正在使用客户机/服务器库,那么通信最终本质上是一个序列化为XML的数据集(从技术上讲,它不是"序列化"的,但它最终大致相似)。所以你从数据集开始,它以膨胀而闻名。此外,数据集将存储每行更改之前和之后的数据-数据量翻倍。然后向XML添加序列化,这会增加更多的开销。然后,该有效负载被包装在web服务调用中——这增加了一点开销。总而言之,您正在通过网络传输非常大量的数据。你所看到的延时只是传输所有数据所花费的时间。

这超出了你的问题,但是解决这个问题的一些建议是:

  • 查看数据在网络上传输时的压缩情况。这里有一些wcf + gzip项目。
  • 您可能想要考虑将文件从数据库移到文件系统中,并使用单独的WCF服务来处理传输文件。