我看过许多解决方案,但没有人帮助解决这个问题。
我有一个应该用于文件上传的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.
答案 0 :(得分:1)
取出使用()。在上传开始/完成之前,您的内存流超出了范围。
答案 1 :(得分:0)
我解决了。
[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>