2015-06-24 85 views
0

我已经看过很多解决方案,但没有人帮助解决此问题。发送流上传文件时,wcf流长度= 0

我有一个应该用于文件上传的wcf服务。

由于我想上传大文件,我没有使用WebHttpRequest,我已经添加了wcf的服务参考,我正在使用它。

这里的服务接口:

[ServiceContract(Namespace = "https://Services.XXX.com", ProtectionLevel = System.Net.Security.ProtectionLevel.None)] 
public interface IAttachmentService 
{ 
    [OperationContract] 
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "UploadFile", Method = "POST", ResponseFormat = WebMessageFormat.Json)] 
    string UploadFile(Stream file); 
} 

,这里是我如何发送该视频流:

using (MemoryStream stream = new MemoryStream()) 
{ 
    //write some data to the the memory stream 
    stream.Write(len, 0, len.Length); 
    stream.Write(jsonFileBytes, 0, jsonFileBytes.Length); 

    //write the file data to the memory stream 
    while ((bytesReadCount = file.Read(bufferRead, 0, bufferRead.Length)) > 0) 
    { 
     stream.Write(bufferRead, 0, bytesReadCount); 
    } 

    stream.Position = 0; 

    byte[] array = stream.ToArray(); 

    WCFClientProxy<Attachment.Interfaces.IAttachmentService> proxy = new WCFClientProxy<Attachment.Interfaces.IAttachmentService>(); 
    return Serialization.ConvertToJson(new { IsError = false, Files = proxy.Instance.UploadFile(array) }); 
} 

但接收,以保存该文件流的方法流的长度为0

我也尝试发送这样的字节[]:

using (MemoryStream stream = new MemoryStream()) 
{ 
    //write some data to the the memory stream 
    stream.Write(len, 0, len.Length); 
    stream.Write(jsonFileBytes, 0, jsonFileBytes.Length); 

    //write the file data to the memory stream 
    while ((bytesReadCount = file.Read(bufferRead, 0, bufferRead.Length)) > 0) 
    { 
     stream.Write(bufferRead, 0, bytesReadCount); 
    } 

    stream.Position = 0; 

    byte[] array = stream.ToArray(); 

    WCFClientProxy<Attachment.Interfaces.IAttachmentService> proxy = new WCFClientProxy<Attachment.Interfaces.IAttachmentService>(); 
    return Serialization.ConvertToJson(new { IsError = false, Files = proxy.Instance.UploadFile(array) }); 
} 

,但这种做法我得到这个错误:

The remote server returned an unexpected response: (400) Bad Request. 
+0

难道ÿ你在'处理一些代码'写完之后回滚流? –

+0

你的意思是这样的:'stream.position = 0;'?如果是这样,比是的,我试过了。在我的'一些代码...'我加入了一些我需要的数据的文件流,因为当通过WCF传递流时,我无法传递任何omre参数 –

+0

'stream.Position = 0'。 –

回答

0

我解决了它。

  1. 删除[WebInvoke...]和接口
  2. 只留下[OperationContract]使用这些配置文件

客户端配置:

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <httpRuntime maxRequestLength="2147483647" /> 
    </system.web> 

    <system.serviceModel> 
    <bindings> 
     <webHttpBinding> 

     <binding name="WebHttpBinding_IAttachmentService" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00" 
       maxBufferSize="2147483647" 
       maxBufferPoolSize="2147483647" 
       maxReceivedMessageSize="2147483647"> 

      <readerQuotas maxDepth="2147483647" 
         maxArrayLength="2147483647" 
         maxBytesPerRead="2147483647" 
         maxNameTableCharCount="2147483647" 
         maxStringContentLength="2147483647"/> 

      <security mode="None" /> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:54893/AttachmentService.svc" 
       binding="webHttpBinding" 
       bindingConfiguration="WebHttpBinding_IAttachmentService" 
       behaviorConfiguration="webHttpBehavior" 
       contract="XXX.Interfaces.IAttachmentService" /> 
    </client> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="webHttpBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <security> 
     <requestFiltering> 
     <requestLimits maxAllowedContentLength="1073741824" /> 
     </requestFiltering> 
    </security> 
    </system.webServer> 

服务配置:

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <httpRuntime maxRequestLength="2147483647" executionTimeout="3600" /> 
    </system.web> 

    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    <services> 
     <service name="XXX.Implementation.AttachmentService"> 
     <endpoint binding="webHttpBinding" 
        behaviorConfiguration="webHttpBehavior" 
        bindingConfiguration="higherMessageSize" 
        contract="XXX.Interfaces.IAttachmentService" /> 
     </service> 
    </services> 
    <bindings> 
     <webHttpBinding> 
     <binding name="higherMessageSize" transferMode="Streamed" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00" 
       maxBufferSize="2147483647" 
       maxBufferPoolSize="2147483647" 
       maxReceivedMessageSize="2147483647"> 

      <readerQuotas maxDepth="2147483647" 
         maxArrayLength="2147483647" 
         maxBytesPerRead="2147483647" 
         maxNameTableCharCount="2147483647" 
         maxStringContentLength="2147483647"/> 

      <security mode="None"/> 
     </binding> 
     </webHttpBinding> 
    </bindings> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="webHttpBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="WebDAVModule"/> 
    </modules> 
     <handlers> 
        <remove name ="WebDAV"/> 
    </handlers> 
    <security> 
     <requestFiltering> 
     <requestLimits maxAllowedContentLength="2147483647" /> 
     </requestFiltering> 
    </security> 
    </system.webServer> 
1

取出使用()。在上传开始/完成之前,您的记忆流将超出范围。

+0

我删除了使用,但错误仍然存​​在... –