简体   繁体   English

采用默认web.config并增加maxStringContentLength所需的更改

[英]Changes needed to take a default web.config and increase maxStringContentLength

I need to send a lot of text from one of my clients to my server. 我需要从我的一个客户端向我的服务器发送大量文本。 To do that I know I need to increase maxStringContentLength on the server. 为此,我知道我需要在服务器上增加maxStringContentLength

I have done a ton of searching on this, and it seems that all the fixes for this start on Step 3 . 我已经对此进行了大量的搜索,似乎所有修复工作都从第3步开始

I can't seem to figure out steps 1 and 2... 我似乎无法弄清楚步骤1和2 ......

Can someone walk me through this nice and slow. 有人可以带我走过这个好又慢的人。 Given my Web.config below, how can I get to set maxStringContentLength ? 鉴于我下面的Web.config,如何设置maxStringContentLength

Here is my Web.config: 这是我的Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>  

Background info: 背景资料:

You will have to modify the properties of the binding used on the service's endpoint. 您必须修改服务端点上使用的绑定的属性 Since you haven't provided any in your configuration file, WCF 4.0 is automatically going to add a default endpoint for each service contract and base address defined in the host. 由于您未在配置文件中提供任何内容,因此WCF 4.0将自动为主机中定义的每个服务协定和基址添加默认端点 If the service is hosted in IIS then the base address is the virtual directory. 如果服务托管在IIS中,则基址是虚拟目录。

The bindings used on the default endpoints are defined within the <protocolMapping> element. 默认端点上使用的绑定在<protocolMapping>元素中定义。 At the machine level, the mappings are: 在机器级别,映射是:

<protocolMapping>
    <add scheme="http" binding="basicHttpBinding"/>
    <add scheme="net.tcp" binding="netTcpBinding"/>
    <add scheme="net.pipe" binding="netNamedPipeBinding"/>
    <add scheme="net.msmq" binding="netMsmqBinding"/>
</protocolMapping>

In your case, unless the default mappings have been overridden, WCF creates a default HTTP endpoint based on the http://myserver/Orders virtual directory using the basicHttpBinding . 在您的情况下,除非已覆盖默认映射,否则WCF将使用basicHttpBinding基于http://myserver/Orders虚拟目录创建默认HTTP端点。

You can modify the maxStringContentLength property for the basicHttpBinding by providing a default binding configuration , that is a nameless configuration, which will automatically be applied to all endpoints using that binding. 您可以通过提供默认绑定配置 (即无名配置)来修改basicHttpBindingmaxStringContentLength属性,该配置将自动应用于使用该绑定的所有端点。

Simply add this element to your Web.config in the <system.serviceModel> section: 只需将此元素添加到<system.serviceModel>部分中的Web.config:

<bindings>
    <basicHttpBinding>
         <binding maxReceivedMessageSize="SomeIntegerValue">
             <readerQuotas maxStringContentLength="SomeIntegerValue" />
        </binding>
    </basicHttpBinding>
</bindings>

Note that MSDN recommends to set both the maxStringContentLength and maxReceivedMessageSize properties to the same value : 请注意, MSDN建议maxStringContentLengthmaxReceivedMessageSize属性设置为相同的值

The value of the maxStringContentLength attribute cannot be greater than the value of the maxReceivedMessageSize attribute. maxStringContentLength属性的值不能大于maxReceivedMessageSize属性的值。 We recommend that the values of the two attributes are the same. 我们建议两个属性的值相同。

If this doesn't work, you can explicitly define which binding to use for the default HTTP endpoint by adding the <protocolMapping> element in your Web.config and adjust the binding configuration accordingly: 如果这不起作用,您可以通过在Web.config中添加<protocolMapping>元素来显式定义要用于默认HTTP端点的绑定,并相应地调整绑定配置:

<protocolMapping>
    <add scheme="http" binding="basicHttpBinding"/>
</protocolMapping>

To give an example of what I was saying in my comment, do the following in the <system.serviceModel> section of your web.config. 要举例说明我在评论中所说的内容,请在web.config的<system.serviceModel>部分中执行以下操作。

First, specify a binding configuration in your config file - for example: 首先,在配置文件中指定绑定配置 - 例如:

<bindings>
  <wsHttpBinding>
    <binding name="MyWsHttpBinding" closeTimeout="00:05:00" 
             openTimeout="00:05:00" 
             receiveTimeout="00:05:00" 
             sendTimeout="00:05:00" 
             transactionFlow="false" 
             hostNameComparisonMode="StrongWildcard" 
             maxBufferPoolSize="524288" 
             maxReceivedMessageSize="5242880">
      <readerQuotas maxDepth="32" 
                    maxStringContentLength="5242880" 
                    maxArrayLength="16384" 
                    maxBytesPerRead="4096" 
                    maxNameTableCharCount="16384" />
    </binding>
  </wsHttpBinding>
</bindings>

Next, you need to assign the above configuration to your endpoint: 接下来,您需要将以上配置分配给您的端点:

<services>
  <service behaviorConfiguration="MyServiceBehavior" name="MyService">
    <endpoint address="" 
              binding="wsHttpBinding" 
              bindingConfiguration="MyWsHttpBinding"
              contract="MyCompany.IMyService" />                       
  </service>
</services>

It's important to remember to assign the name of the binding configuration to the endpoint via the bindingConfiguration , otherwise you'll get the default values for the selected binding. 记住通过bindingConfiguration将绑定配置的名称分配给端点bindingConfiguration ,否则您将获得所选绑定的默认值。

Also, check out the following article regarding default endpoints and bindings for WCF 4.0 - A Developer's Introduction to Windows Communication Foundation 4 另外,请查看以下有关WCF 4.0的默认端点和绑定的文章 - 开发人员的Windows Communication Foundation简介4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM