将流发送到上传文件时,wcf流长度=0

本文关键字:wcf 文件 | 更新日期: 2023-09-27 17:59:31

我看过很多解决方案,但都没有帮助解决这个问题。

我有一个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

我也尝试过像这样发送byte[]:

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.

将流发送到上传文件时,wcf流长度=0

去掉using()。在上传开始/完成之前,您的内存流已超出范围。

我解决了它。

  1. 删除[WebInvoke...],只在接口中保留[OperationContract]
  2. 使用这些配置文件

客户端配置:

<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>