本文介绍了大二进制 (byte[]) 文件通过 WCF 传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个 WCF 服务,该服务允许我将大型二进制文件从客户端发送到该服务.

但是我最多只能成功传输 3-4MB 的文件.(当我尝试传输 4.91MB 以及当然超出的任何内容时,我失败了)

如果我尝试发送 4.91MB 的文件,我得到的错误是:

异常消息:接收对 http://的 HTTP 响应时出错本地主机:56198/Service.svc.这可能是由于服务端点绑定未使用 HTTP 协议.这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭).有关详细信息,请参阅服务器日志.

内部异常消息:底层连接已关闭:接收时发生意外错误.

内部异常消息:无法从传输连接读取数据:远程主机强行关闭了现有连接.

内部异常消息:现有连接被远程主机强行关闭

只要将 byte[] 文件作为方法参数发送到公开的服务方法,就会在客户端发生此错误.

我在服务方法的第一行有一个断点,万一文件传输成功(低于 3MB),断点被击中并传输文件.但是在这种情况下,只要调用该方法,就会出现错误.出现此错误时不会命中服务中的断点.

我将粘贴我的服务 Web.config 和 Asp 页面(客户端)Web.config 的部分.如果您还需要发送文件和接受文件的代码,请告诉我,我也会发送.

服务 Web.Config

<绑定><基本HttpBinding><绑定名称="basicHttpEndpointBinding" closeTimeout="01:01:00"openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"messageEncoding="Mtom" textEncoding="utf-8" transferMode="StreamedRequest"useDefaultWebProxy="true"><readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646"/><安全模式=无"><transport clientCredentialType="None" proxyCredentialType="None"领域=""/><message clientCredentialType="UserName" algorithmSuite="默认"/></安全></binding></basicHttpBinding></绑定><服务><service behaviorConfiguration="DragDrop.Service.ServiceBehavior" name="DragDrop.Service.Service"><端点地址="" binding="basicHttpBinding" bindingConfiguration="basicHttpEndpointBinding" contract="DragDrop.Service.IService"><身份><dns value="localhost"/></身份></端点><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/></服务></服务><行为><服务行为><行为名称="DragDrop.Service.ServiceBehavior"><serviceMetadata httpGetEnabled="true"/><serviceDebug includeExceptionDetailInFaults="false"/><dataContractSerializer maxItemsInObjectGraph="2147483646"/></行为></serviceBehaviors></行为></system.serviceModel>

客户端(Asp.net 页面)Web.Config

<绑定><基本HttpBinding><绑定名称="BasicHttpBinding_IService" closeTimeout="00:01:00"openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"messageEncoding="Mtom" textEncoding="utf-8" transferMode="StreamedResponse"useDefaultWebProxy="true"><readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646"/><安全模式=无"><transport clientCredentialType="None" proxyCredentialType="None"领域=""><extendedProtectionPolicy policyEnforcement="Never"/></运输><message clientCredentialType="UserName" algorithmSuite="默认"/></安全></binding></basicHttpBinding></绑定><行为><端点行为><行为名称=调试行为"><dataContractSerializer maxItemsInObjectGraph="2147483646"/></行为></endpointBehaviors></行为><客户><端点地址="http://localhost:56198/Service.svc" binding="basicHttpBinding"bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference.IService"name="BasicHttpBinding_IService" behaviorConfiguration="debuggingBehaviour"/></客户端></system.serviceModel>
解决方案

(虽然我同意 流式传输 会更可取,以下内容应该可以使其在没有任何其他更改的情况下工作)

您还需要在 Web.config 中增加最大消息长度:

<system.web><httpRuntime maxMessageLength="409600"executionTimeoutInSeconds="300"/></system.web></配置>

这会将最大消息长度设置为 400 MB(参数单位为 kB).查看此 MSDN 页面了解更多信息.

I am trying to build a WCF service that allows me to send large binary files from clients to the service.

However I am only able to successfully transfer files up to 3-4MB. (I fail when I try to transfer 4.91MB and, off course, anything beyond)

The Error I get if I try to send the 4.91MB file is:

Exception Message: An error occurred while receiving the HTTP response to http://localhost:56198/Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Inner Exception Message: The underlying connection was closed: An unexpected error occurred on a receive.

Inner Exception Message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Inner Exception Message: An existing connection was forcibly closed by the remote host

This error occurs at client side as soon as the byte[] file is sent as a method parameter to the exposed service method.

I have a breakpoint at the service method's first line, in case of successful file transfers (below 3MB) that break point is hit and the file gets transferred. However in this case as soon as the method is called, the error comes. The breakpoint in the service is not hit in case of this error.

I am going to paste my sections of my Service Web.config and Asp Page (Client) Web.config. If you also require the code that send the file and accepts the file, let me know, I'll send that as well.

Service Web.Config

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicHttpEndpointBinding" closeTimeout="01:01:00"
      openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"
      messageEncoding="Mtom" textEncoding="utf-8" transferMode="StreamedRequest"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"
        maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
    <services>
        <service behaviorConfiguration="DragDrop.Service.ServiceBehavior" name="DragDrop.Service.Service">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpEndpointBinding" contract="DragDrop.Service.IService">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="DragDrop.Service.ServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Client (Asp.net page) Web.Config

<system.serviceModel>
<bindings>
   <basicHttpBinding>
      <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
         allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
         maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"
         messageEncoding="Mtom" textEncoding="utf-8" transferMode="StreamedResponse"
         useDefaultWebProxy="true">
         <readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"
            maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" />
         <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
               realm="">
               <extendedProtectionPolicy policyEnforcement="Never" />
            </transport>
            <message clientCredentialType="UserName" algorithmSuite="Default" />
         </security>
      </binding>
   </basicHttpBinding>
</bindings>

<behaviors>
  <endpointBehaviors>
    <behavior name="debuggingBehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483646" />
    </behavior>
  </endpointBehaviors>
</behaviors>

<client>
   <endpoint address="http://localhost:56198/Service.svc" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference.IService"
      name="BasicHttpBinding_IService" behaviorConfiguration="debuggingBehaviour" />
</client>
</system.serviceModel>
解决方案

(While I agree that streaming transfer would be preferrable, the below should make it work without any other changes)

You also need to increase the maximum message length in the Web.config:

<configuration>
  <system.web>
  <httpRuntime maxMessageLength="409600"
    executionTimeoutInSeconds="300"/>
  </system.web>
</configuration>

This will set the maximum message length to 400 MB (parameter is in kB). Check this MSDN page for more information.

这篇关于大二进制 (byte[]) 文件通过 WCF 传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 12:41
查看更多