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

C# 如何将大型数据集发送到WCF服务?,c#,wcf,web-services,C#,Wcf,Web Services,我想从WPF应用程序向WCF服务发送一个DataSet。当我发送一个空的数据集时,它工作正常。但是DataSet和30DataTable它给了我一个例外 远程服务器返回意外响应:(400)错误请求 我的经营合同是: [OperationContract] string InsertDataToDatabase(DataSet ds); 将数据集发送到WCF服务的最佳方式是什么 客户端App.Config <?xml version="1.0" encoding="utf-8" ?

我想从WPF应用程序向WCF服务发送一个
DataSet
。当我发送一个空的
数据集
时,它工作正常。但是
DataSet
和30
DataTable
它给了我一个例外

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

我的经营合同是:

  [OperationContract]
  string InsertDataToDatabase(DataSet ds);
数据集
发送到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>
<?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>

我不记得曾经看到过数据大小问题的400错误消息,但是…有一些事情你可以尝试

这里发生了几件事。首先,根据服务的配置文件,您在.NET4.0上。这意味着您使用的是默认端点——您没有在配置中明确定义一个端点,因此框架会为您创建一个端点。并且它使用相应绑定的默认值(
BasicHttpBinding
),这意味着即使更改定义的绑定中的值,它们也不会生效。此外,您还将收到带有默认值的默认行为

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

现在,由于没有显式定义端点,因此需要创建端点并将定义的绑定分配给它,或者将定义设为默认值

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

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ICommunication" 
             maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
请注意,在上述(剥离的)配置中没有
name
属性

第二种方法是创建端点并为其分配定义的绑定配置。例如:

<bindings>
  <basicHttpBinding>
    <binding maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="MyServiceName">
      <endpoint address="" 
                binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_ICommunication"
                contract="MyService.IMyContract" />
  </service>
</services>
同样,由于您使用的是.NET 4.0,您需要将此行为定义为默认行为(通过省略
name
属性)或将其分配给已定义的端点,如下所示(使用上面的端点示例):



您也可以看看。

我不记得曾经看到过数据大小问题的400错误消息,但是……有一些事情您可以尝试

这里发生了几件事。首先,根据服务的配置文件,您在.NET4.0上。这意味着您使用的是默认端点——您没有在配置中明确定义一个端点,因此框架会为您创建一个端点。并且它使用相应绑定的默认值(
BasicHttpBinding
),这意味着即使更改定义的绑定中的值,它们也不会生效。此外,您还将收到带有默认值的默认行为

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

现在,由于没有显式定义端点,因此需要创建端点并将定义的绑定分配给它,或者将定义设为默认值

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

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ICommunication" 
             maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
请注意,在上述(剥离的)配置中没有
name
属性

第二种方法是创建端点并为其分配定义的绑定配置。例如:

<bindings>
  <basicHttpBinding>
    <binding maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="MyServiceName">
      <endpoint address="" 
                binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_ICommunication"
                contract="MyService.IMyContract" />
  </service>
</services>
同样,由于您使用的是.NET 4.0,您需要将此行为定义为默认行为(通过省略
name
属性)或将其分配给已定义的端点,如下所示(使用上面的端点示例):



您还可以查看。

您需要调整服务的绑定设置。你能发布你的服务配置文件吗?如果不知道web服务有什么问题,就很难说。你需要先调查一下。如果这是第三方服务,请请求信息(或web服务的可能限制)。如果是您的服务,请先调查您自己。@Strelok,它是我的本地服务,我已使用空数据集进行了测试。您的服务可能引发了异常。查看事件日志。此外,除了.NET客户端之外,其他客户端是否会使用此服务?那么DataSet不是一个好的选择。在任何情况下,一组对象对用户来说都比数据集更方便。您需要调整服务的绑定设置。你能发布你的服务配置文件吗?如果不知道web服务有什么问题,就很难说。你需要先调查一下。如果这是第三方服务,请请求信息(或web服务的可能限制)。如果是您的服务,请先调查您自己。@Strelok,它是我的本地服务,我已使用空数据集进行了测试。您的服务可能引发了异常。查看事件日志。此外,除了.NET客户端之外,其他客户端是否会使用此服务?然后是数据