如何将大型数据集发送到WCF服务?

Ars*_*had 0 c# wcf web-services

我想DataSet从WPF应用程序发送到WCF服务.当我发送一个空的DataSet它工作正常.但是DataSet30岁的DataTable它给了我一个例外

远程服务器返回意外响应:(400)错误请求.

我的运营合同是:

  [OperationContract]
  string InsertDataToDatabase(DataSet ds);
Run Code Online (Sandbox Code Playgroud)

哪个是发送DataSet到WCF服务的最佳方式?

客户 App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ICommunication" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192"  maxArrayLength="2147483647"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:49486/Communication.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICommunication"
                contract="CommunicationReference.ICommunication" name="BasicHttpBinding_ICommunication" />
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

服务 Web.Config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <connectionStrings>
    <add name="ConName" connectionString="Data Source=WIN-7;Initial Catalog=ABC; User ID=sa; Password=****"/>        
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647">
          <readerQuotas maxStringContentLength="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>      
      <serviceBehaviors>
        <behavior>
           <!--To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment--> 
          <serviceMetadata httpGetEnabled="true"/>
           <!--To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information--> 
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="1000" maxConcurrentInstances="1600" />
        </behavior>
      </serviceBehaviors>
    </behaviors>    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>  
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 5

我不记得有任何关于数据大小问题的400错误消息,但是......你可以尝试一些事情.

这里有几件事情.首先,根据您的服务配置文件,您使用的是.NET 4.0.这意味着您正在使用默认端点 - 您没有在配置中明确定义端点,因此框架会为您创建一个端点.它使用适当绑定的默认值(BasicHttpBinding在本例中),这意味着即使您更改了您定义的绑定中的值,它们也不会生效.此外,您还将收到默认值的默认行为.

尝试maxReceivedMessageSize在绑定中设置更高的值- 默认值为65536 - 和maxStringContentLength(默认值为8192~8K).如果增加大小,maxStringContentLength还需要将大小maxBufferSize增加到相同的值(至少).

现在,由于您没有明确定义端点,因此您需要创建端点并为其定义绑定,或者使定义具有默认值.

首先,绑定的更新版本(用于处理更大的数据)可能如下所示(仅显示需要更改的相关部分 - 为简洁起见,我省略了其余部分).我选择了一个相当大的数字 - for maxBufferSizemaxReceivedMessageSizemax是Int64 maxStringContentLength的最大值,而最大值是Int32的最大值.

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ICommunication" 
             maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

接下来,您需要将此绑定分配给服务端点.最简单的方法,特别是如果您只有一个端点,是使绑定定义高于默认值.您只需省略元素中的name属性即可完成此操作binding.例如:

<bindings>
  <basicHttpBinding>
    <binding maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

请注意name,上面(剥离)配置中没有属性.

第二种方法是创建端点并将您定义的绑定配置分配给它.例如:

<services>
  <service name="MyServiceName">
      <endpoint address="" 
                binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_ICommunication"
                contract="MyService.IMyContract" />
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

我也看到人们在服务行为中增加了元素的maxItemsInObjectGraph属性dataContractSerializer,尽管我从来没有这样做过.根据MSDN文档,我不认为这适用于您的情况,因为您没有使用任何数据合同.但是,要尝试为您提供尽可能多的选项来解决此问题,您可以通过在行为组中定义端点行为进行调整,如下所示:

<behaviors>
  <endpointBehaviors>
    <behavior name="LargeDataBehavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483646" />
    </behavior>
  </endpointBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

同样,由于您使用的是.NET 4.0,您需要将此行为定义为默认行为(通过省略该name属性)或将其分配给定义的端点,如下所示(使用上面的端点示例):

<endpoint address=""
          behaviorConfiguration="LargeDataBehavior" 
          binding="basicHttpBinding" 
          bindingConfiguration="BasicHttpBinding_ICommunication"
          contract="MyService.IMyContract" />
Run Code Online (Sandbox Code Playgroud)

您还可以查看WCF Streaming.