从桌面应用程序(.Net 2)调用WebService(WCF)时的消息大小

时间:2017-05-09 13:28:58

标签: c# .net web-services wcf soap

我有一个Web服务(WCF),它将收到的文件存储在数据库中。 当我从.NET 4.5 Web应用程序调用它时,我只在客户端的Web.config文件中指定:

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_NameOfWS"  
             maxBufferSize="2147483647" 
             maxReceivedMessageSize="2147483647" />
  </basicHttpBinding>
</bindings>

因此允许客户端发送大文件。

当我尝试从我的.NET 2桌面应用程序(是的,.NET 2)调用它时,我收到错误:

HTTP 413: Request Entity Too Large

但我没有配置Web.config文件,因为它不是一个Web应用程序。

我的app.config看起来像:

<applicationSettings>
    <NameOfProject.Properties.Settings>
        <setting name="NameOfProject_NameOfWebReference_NameOfWS"
            serializeAs="String">
            <value>http://myURL/NameOfWS.svc</value>
        </setting>
    </NameOfProject.Properties.Settings>
</applicationSettings>

NameOfWS.wsdl文件没有maxBufferSize或maxReceivedMessageSize参数。

我错过了什么吗?

提前致谢。

3 个答案:

答案 0 :(得分:1)

对于app.config绑定配置示例

请尝试其中一些方法;

 <binding name="bindingName" closeTimeout="00:20:00" openTimeout="00:20:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport" />
    </binding>

答案 1 :(得分:0)

在你的 .net 2 app.config上写这个它必须是接收文件,但我建议你复制 .net 4 app.config 标记为 .net 2 app.config 它必须正常工作

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="10485760">
        <readerQuotas ... />
      </binding>
    </basicHttpBinding>
  </bindings>  
</system.serviceModel>

答案 2 :(得分:0)

解决!

如果有人需要解决这样的问题,我会发布如何让它发挥作用。

我的服务项目中的Web.config

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_MyWCFInterface"  maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="MyProject.MyWCFClass" >
        <endpoint address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBinding_MyWCFInterface"
                  contract="MyProject.MyWCFInterface"/>
      </service>
    </services>
</system.serviceModel>

我的客户项目中的App.config

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
          <binding name="BasicHttpBinding_MyWCFInterface" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      </binding>
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:myPort/MyService.svc" 
              binding="basicHttpBinding" 
              bindingConfiguration="BasicHttpBinding_MyWCFInterface" 
              contract="NameOfCreatedService.MyWCFInterface" 
              name="BasicHttpBinding_MyWCFInterface" />
  </client>
</system.serviceModel>

我不知道并且是最后一步,在我客户解决方案的主要项目的App.config中复制此App.config配置,如果不是,配置在启动时没有加载,并且不能处理消息大小配置。

我希望它可以帮助任何人。

感谢那些花了一些时间解决这个问题的人。