WCF,已超过传入消息的最大消息大小配额(65536)

本文关键字:消息 65536 WCF | 更新日期: 2023-09-27 18:00:23

这个问题可能已经有了答案,但没有一个对我有效。我是WCF的新手,这是我工作的第一个项目。

我试过的答案:

已超过传入消息的最大消息大小配额(65536)

Wcf是否已超过传入邮件的最大邮件大小配额(65536)?

WCF错误-已超过传入消息的最大消息大小配额(65536)

我已经创建了一个项目,并在本地机器上成功运行。当我在IIS上发布并在Windows窗体应用程序下运行它时,我会收到以下错误:

已超过传入邮件的最大邮件大小配额(65536)。若要增加配额,请对相应的绑定元素使用MaxReceivedMessageSize属性。

这是我的服务器(WCF)web.config:

<?xml version="1.0"?>
<configuration>
   <appSettings>
      <add key="connectionString" value="data source=localhost; initial catalog=TWO; integrated security=SSPI"/>
   </appSettings>
   <system.web>
       <compilation debug="true" targetFramework="4.0" />
       <pages validateRequest="false" />
       <httpRuntime requestValidationMode="2.0" />
   </system.web>
   <system.serviceModel>
      <services>
         <service name="Service1.IService1">
            <endpoint 
                address="" 
                binding="basicHttpBinding" 
                contract="Service1.IService1">
            </endpoint>
            <host>
               <baseAddresses>
                   <add baseAddress="http://localhost:50935/Service1.svc"/>
               </baseAddresses>
           </host>
       </service>
   </services>
   <bindings>
      <basicHttpBinding>
         <binding  
              maxBufferPoolSize="2147483647" maxBufferSize="2147483647" 
              maxReceivedMessageSize="2147483647" messageEncoding="Text">
            <readerQuotas maxDepth="2000000" 
                   maxStringContentLength="2147483647" 
                   maxArrayLength="2147483647" 
                   maxBytesPerRead="2147483647" 
                   maxNameTableCharCount="2147483647" />
         </binding>
      </basicHttpBinding>
   </bindings>
   <behaviors>
       <serviceBehaviors>
           <behavior>
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
           </behavior>
       </serviceBehaviors>
       <endpointBehaviors>
           <behavior name="behaviorGPLineItemsService">
               <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
           </behavior>
       </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

这是我的客户(Winforms)app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
  <httpRuntime maxRequestLength="2147483647"/>
</system.web>
<system.serviceModel>
  <client>
    <endpoint address="http://192.168.0.60/Service1.svc" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1" contract="GPLineItemsService.IService1"
      name="BasicHttpBinding_IService1" />
  </client>
  <bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="GpWebServiceBehavior">
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>
</configuration>

这是我用来调用服务的客户端代码

EndpointAddress address = new EndpointAddress(GPWCFEndPointAddress);
BasicHttpBinding binding = new BasicHttpBinding();
GPLineItemsService.Service1Client gpService = new GPLineItemsService.Service1Client(binding, address);
GPLineItemsService.GPItems gpItems = new GPLineItemsService.GPItems();
gpItems = gpService.InsertUpdateLineItemsInGP(dtGPItems);
opResult = gpItems.ErrorGPItems;

非常感谢您的帮助。谢谢

WCF,已超过传入消息的最大消息大小配额(65536)

您在代码中创建自己的绑定,而不是使用配置文件中指定的绑定,因此您在应用程序配置文件中所做的任何更改基本上都会被忽略。

您可以在如下代码中设置maxReceivedMessageSize值:

binding.MaxReceivedMessageSize = 2147483647;

这使你的完整代码块这个:

EndpointAddress address = new EndpointAddress(GPWCFEndPointAddress);
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = 2147483647;
GPLineItemsService.Service1Client gpService = 
    new GPLineItemsService.Service1Client(binding, address);
GPLineItemsService.GPItems gpItems = new GPLineItemsService.GPItems();
gpItems = gpService.InsertUpdateLineItemsInGP(dtGPItems);
opResult = gpItems.ErrorGPItems;

或者,您可以使用配置文件中指定的绑定和端点:

GPLineItemsService.Service1Client gpService = new GPLineItemsService.Service1Client();
GPLineItemsService.GPItems gpItems = new GPLineItemsService.GPItems();
gpItems = gpService.InsertUpdateLineItemsInGP(dtGPItems);
opResult = gpItems.ErrorGPItems;

这应该是<httpRuntime maxRequestLength="32768"/>的问题。请尝试设置更大的值。