我想从WPF应用程序向WCF服务发送DataSet
。当我发送空DataSet
时,它可以正常工作。但是DataSet
有30 DataTable
,它给了我
远程服务器返回了意外响应:(400)Bad Request。
我的运营合同是:
[OperationContract]
string InsertDataToDatabase(DataSet ds);
哪个是将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>
服务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>
答案 0 :(得分:5)
我不记得有任何关于数据大小问题的400错误消息,但是......你可以尝试一些事情。
这里有几件事情要发生。首先,根据您的服务配置文件,您使用的是.NET 4.0。这意味着您正在使用默认端点 - 您没有在配置中明确定义端点,因此框架会为您创建一个端点。它使用适当绑定的默认值(在这种情况下为BasicHttpBinding
),这意味着即使您更改了您定义的绑定中的值,它们也不会生效。此外,您还将收到默认值的默认行为。
尝试在绑定中为maxReceivedMessageSize
设置更高的值 - 默认值为65536 - 和maxStringContentLength
(默认值为8192~8K)。如果您增加maxStringContentLength
的大小,则还需要将maxBufferSize
的大小增加到相同的值(至少)。
现在,由于您没有明确定义端点,因此您需要创建端点并为其定义绑定,或者使定义具有默认值。
首先,绑定的更新版本(用于处理更大的数据)可能如下所示(仅显示需要更改的相关部分 - 为简洁起见,我省略了其余部分)。我选择了一个相当大的数字 - 对于maxBufferSize
和maxReceivedMessageSize
,最大值是Int64的最大值,对于maxStringContentLength
,最大值是Int32的最大值。< / p>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICommunication"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
接下来,您需要将此绑定分配给服务端点。最简单的方法,特别是如果您只有一个端点,是使绑定定义高于默认值。您只需省略name
元素中的binding
属性即可。例如:
<bindings>
<basicHttpBinding>
<binding maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
请注意,上述(剥离)配置中没有name
属性。
第二种方法是创建端点并为其定义绑定配置。例如:
<services>
<service name="MyServiceName">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICommunication"
contract="MyService.IMyContract" />
</service>
</services>
我也看到人们在服务行为中增加maxItemsInObjectGraph
元素的dataContractSerializer
属性,尽管我从未这样做过。根据MSDN文档,我不认为这适用于您的情况,因为您没有使用任何数据合同。但是,要尝试为您提供尽可能多的选项来解决此问题,您可以通过在行为组中定义端点行为进行调整,如下所示:
<behaviors>
<endpointBehaviors>
<behavior name="LargeDataBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
</behavior>
</endpointBehaviors>
</behaviors>
同样,由于您使用的是.NET 4.0,因此您需要将此行为定义为默认行为(通过省略name
属性)或将其分配给定义的端点,如此(使用上面的端点示例):
<endpoint address=""
behaviorConfiguration="LargeDataBehavior"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICommunication"
contract="MyService.IMyContract" />
您也可以查看WCF Streaming。