2009-10-05 108 views
5

如何发送大文件从客户端到服务器在C#中使用WCF?在配置代码下面。如何使用WCF将大型文件从客户端发送到服务器?

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="HttpStreaming_IStreamingSample" 
         maxReceivedMessageSize="67108864" 
          transferMode="Streamed"> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint 
      address="http://localhost:4127/StreamingSample.svc" 
      binding="basicHttpBinding" 
      bindingConfiguration="HttpStreaming_IStreamingSample" 
      contract="StreamingSample.IStreamingSample" 
      name="HttpStreaming_IStreamingSample" /> 
    </client> 
</system.serviceModel> 
+0

好的,这是客户端配置。请同时显示服务器配置和服务合同(你打电话给你的方法是什么?) – 2009-10-06 11:25:13

回答

6

如Dzmitry已经指出的那样,您需要检查流式传输。

为了能够发送大文件的流为您服务,您需要:

  • 创建一个接受Stream作为它的输入参数
  • 创建绑定服务的方法配置(服务器和客户端上),它采用transferMode=StreamedRequest
  • 在客户端创建一个流,并将其发送给服务方法

所以首先,你需要在你的服务合同的方法:

[ServiceContract] 
interface IYourFileService 
{ 
    [OperationContract] 
    void UploadFile(Stream file) 
} 

然后,你需要一个绑定配置:

<bindings> 
    <basicHttpBinding> 
    <binding name="FileUploadConfig" 
      transferMode="StreamedRequest" /> 
    </basicHttpBinding> 
</bindings> 

,并使用该绑定配置你的服务的服务端点:

<services> 
    <service name="FileUploadService"> 
    <endpoint name="UploadEndpoint" 
       address="......." 
       binding="basicHttpBinding" 
       bindingConfiguration="FileUploadConfig" 
       contract="IYourFileService" /> 
    </service> 
</services> 

然后,在您的客户端中,您需要打开例如一个文件流并将其发送到服务方法而不关闭它。

希望有帮助!

马克

+0

感谢这篇文章。我已经试过这个,但它会引发下面的异常:“远程服务器返回一个错误:(400)错误的请求” – Charan 2009-10-05 12:21:51

+0

,听起来像你的配置是不好的 - 你可以发布你现在的客户端和服务器配置你原来的问题?只是部分。谢谢! – 2009-10-05 12:29:07

+0

<绑定名称= “HttpStreaming” maxReceivedMessageSize = “67108864” transferMode = “缓冲”> <服务名称= “WCFFileStreamingDemo.StreamingSample” behaviorConfiguration = “WCFFileStreamingDemo.StreamingSampleBehavior”> <端点地址= “” 结合= “basicHttpBinding的” bindingName = “HttpStreaming” 合同= “WCFFileStreamingDemo.IStreamingSample”> <端点地址= “MEX” 结合= “mexHttpBinding” 合同= “IMetadataExchange接口”/> – Charan 2009-10-05 14:40:46

2

除了增加readerQuota设置(如上所述),我不得不也起来了maxRequestLength所述的httpRuntime属性内。

<system.web> 
    <httpRuntime maxRequestLength="2097151" /> 
</system.web> 
相关问题