C# 将 MaxReceivedMessageSize 属性放在 WCF 服务的 web.config 文件中的什么位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9967053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put MaxReceivedMessageSize property in WCF service's web.config file?
提问by marko
I need to change my web.configfile and add the MaxReceivedMessageSizeproperty in
my web.config- but where?
我需要更改我的web.config文件并将MaxReceivedMessageSize属性添加到我的web.config- 但在哪里?
The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
已超出传入邮件的最大邮件大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false"><assemblies><add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /></assemblies></compilation>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
采纳答案by marc_s
You need to define a binding configuration for the binding you want to use and then you need to define your services (on the server-side) and clients (on the client side) to use that binding and binding configuration:
您需要为要使用的绑定定义绑定配置,然后需要定义服务(在服务器端)和客户端(在客户端)以使用该绑定和绑定配置:
<system.serviceModel>
<bindings>
<!-- pick whichever binding you want .... -->
<basicHttpBinding>
<!-- binding configuration with a name -->
<binding name="ExtendedMaxSize"
maxBufferSize="999999" maxReceivedMessageSize="999999" />
</basicHttpBinding>
</bindings>
<services>
<service name="Yournamespace.YourServiceClass" behaviorConfiguration="...">
<!-- define endpoint with your binding and the name of the binding configuration
that you have defined just above -->
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="ExtendedMaxSize"
contract="Yournamespace.IYourServiceContract" />
</service>
</services>
回答by mly
To help those who may end up here like I did. I cannot add to the comments above yet (Usually someone already has the answers long before I have the problem), so I have to add an answer.
帮助那些可能像我一样最终来到这里的人。我还不能添加到上面的评论中(通常在我遇到问题之前很久就有人已经有了答案),所以我必须添加一个答案。
I have an MVC 4 app, and I suspect the initial sample above is from the web.config of the actual WCF service project. One of the comments mentions they suspect it is an MVC 4 app and the default config settings.
我有一个 MVC 4 应用程序,我怀疑上面的初始示例来自实际 WCF 服务项目的 web.config。其中一条评论提到他们怀疑这是一个 MVC 4 应用程序和默认配置设置。
But how do you fix the problem? From more research, it appears that the change actually needs to be made to the web.config for the CLIENT, in other words, the web config for the project with the REFERENCE to the WCF service. You will find it is much easier to make the change there. That version of the web.config will actually resemble what you are looking for.
但是你如何解决这个问题呢?从更多的研究来看,似乎实际上需要对 CLIENT 的 web.config 进行更改,换句话说,就是对 WCF 服务具有 REFERENCE 的项目的 web 配置。您会发现在那里进行更改要容易得多。该版本的 web.config 实际上与您要查找的内容相似。
That worked easily for me and fixed my issue.
这对我来说很容易并解决了我的问题。
回答by Sergei Meleshchuk
- No need, contrary to often claimed, to set on the server.
- Contrary to what MSDN is saying, it is not enough to set the limit on the transport binding element. Need to set on binding itself too. For example:
- 与经常声称的相反,无需在服务器上进行设置。
- 与 MSDN 所说的相反,仅对传输绑定元素设置限制是不够的。也需要设置绑定本身。例如:
var targetBinding = new BasicHttpsBinding();
targetBinding.MaxReceivedMessageSize = MaxWcfMessageSize;
targetBinding.MaxBufferPoolSize = MaxWcfMessageSize;
targetBinding.MaxBufferSize = MaxWcfMessageSize;
var targetBindingElements = targetBinding.CreateBindingElements();
var httpsBindElement = targetBindingElements.Find<HttpsTransportBindingElement>();
httpsBindElement.MaxReceivedMessageSize = MaxWcfMessageSize;
httpsBindElement.MaxBufferPoolSize = MaxWcfMessageSize;
httpsBindElement.MaxBufferSize = MaxWcfMessageSize;
TextMessageEncodingBindingElement tmbebe = targetBindingElements.Find<TextMessageEncodingBindingElement>();
tmbebe.ReaderQuotas.MaxArrayLength = MaxWcfMessageSize;

