发送流上传文件时wcf stream length = 0
wcf stream length = 0 when sending stream to upload file
我看过很多解决方案,但 none 帮助解决了这个问题。
我有一个应该用于文件上传的 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.
去掉using()。您的内存流在上传前超出范围 starts/completes。
我解决了。
- 删除
[WebInvoke...]
,只保留界面中的[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>
我看过很多解决方案,但 none 帮助解决了这个问题。
我有一个应该用于文件上传的 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.
去掉using()。您的内存流在上传前超出范围 starts/completes。
我解决了。
- 删除
[WebInvoke...]
,只保留界面中的[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>