0

我在我的应用程序中使用 WCF。从应用程序我通过 WCF 传递一个列表以在后端处理。如果列表中的行数很大,则服务会抛出异常“远程服务器返回意外响应:(400) 错误请求。”

下面是我的 UI 网络配置

<system.serviceModel>

<bindings>

  <basicHttpBinding>

    <binding name="BasicHttpBinding_IService1" closeTimeout="10:20:00"
      openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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="Windows" proxyCredentialType="Windows"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />

      </security>

    </binding>
    <binding name="BasicHttpBinding_IErrorService" closeTimeout="10:20:00"
      openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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>
    <binding name="BasicHttpBinding_IService11" closeTimeout="10:20:00"
      openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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="Windows" proxyCredentialType="Windows"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />

      </security>

    </binding>
    <binding name="BasicHttpBinding_ISecurityUserService" closeTimeout="10:20:00"
      openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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="Windows" proxyCredentialType="Windows"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />

      </security>

    </binding>

  </basicHttpBinding>
</bindings>

<client>

  <endpoint address="http://localhost:6789/Service1.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
    name="BasicHttpBinding_IService1" />
  <endpoint address="http://localhost:6789/ErrorService.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IErrorService" contract="ErrorServiceReference.IErrorService"
    name="BasicHttpBinding_IErrorService" />
  <endpoint address="http://localhost:6789/SecurityUserService.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISecurityUserService"
    contract="SecurityUserServiceReference.ISecurityUserService"
    name="BasicHttpBinding_ISecurityUserService" />
  <endpoint address="http://localhost:6789/Service1.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IService11" contract="ServiceReference.IService1"
    name="BasicHttpBinding_IService11" />

</client>

</system.serviceModel>

还将以下标记添加到 WCF Web 配置以及 UI Web 配置

<system.web>

      <httpRuntime maxRequestLength="512000" />

  </system.web>

我的服务配置如下

  <httpRuntime maxRequestLength="512000" />

服务模式:

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>      
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<modules runAllManagedModulesForAllRequests="true"/>

我修改了服务 Web 配置 。我仍然遇到同样的问题。请帮助我

<services>

  <service name="BasicHttpBinding_IService1" behaviorConfiguration="Behavior">
    <endpoint address=""
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService1"
              contract="ServiceReference1.IService1"/>        
  </service>
  <service name="BasicHttpBinding_IErrorService" behaviorConfiguration="Behavior">
    <endpoint address=""
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IErrorService"
              contract="ErrorServiceReference.IErrorService"/>

  </service>
  <service name="BasicHttpBinding_ISecurityUserService" behaviorConfiguration="Behavior">
    <endpoint address=""
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_ISecurityUserService"
              contract="SecurityUserServiceReference.ISecurityUserService"/>

  </service>

</services>

and bindings

<bindings>

 <basicHttpBinding>
  <binding name="BasicHttpBinding_IService1" closeTimeout="10:20:00"
    openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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="Windows" proxyCredentialType="Windows"
        realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
  </binding>
    <binding name="BasicHttpBinding_IErrorService" closeTimeout="10:20:00"
      openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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>
    <binding name="BasicHttpBinding_ISecurityUserService" closeTimeout="10:20:00"
      openTimeout="10:20:00" receiveTimeout="10:20:00" sendTimeout="10: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="Windows" proxyCredentialType="Windows"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>

</bindings>
4

1 回答 1

0

您的服务配置中似乎没有定义任何端点或绑定,这意味着您正在获取默认端点和绑定。由于您的服务使用默认绑定及其默认值,因此它无法接收大消息。

您可以通过省略name绑定元素中的属性来覆盖默认绑定设置。但是,在您的情况下,您在三个定义的绑定中有不同的设置(至少在安全部分)。因此,您需要定义绑定并将它们分配给端点。

尝试这样的事情:

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" .....>
  </basicHttpBinding>
</bindings>

我省略了大部分绑定声明——你可以简单地从你的客户端配置中复制它们。

bindingConfig然后在服务中使用端点元素上的属性分配特定的绑定配置,如下所示:

<services>
  <service .... >
    <endpoint address="http://localhost:6789/Service1.svc"  
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService1" 
              contract="ServiceReference1.IService1" 
              name="BasicHttpBinding_IService1" />
  </service>
</services>

同样,为简单起见,我省略了大部分配置代码。

这里的关键点是服务需要有一个绑定,该绑定被配置为接受比默认值更大的消息。增加maxRequestLength运行时的值不会直接影响 WCF 服务(当然,除非您的请求大于 HTTP 运行时值)。

您可以在此处阅读有关默认端点和绑定的更多信息 - A Developer's Introduction to Windows Communication Foundation 4

于 2013-04-10T21:30:15.770 回答