有人可以看看我的新代碼嗎?我卡在這個bug 3天現在和它的駕駛我瘋狂......WCF - 大文件上傳 - (413)請求實體太大
我想有一個WCF文件上傳,但大文件沒有上傳
我得到這個錯誤:「遠程服務器返回了意外的響應:(413)請求實體太大。」
我的項目是由3個部分組成內置:
WCF服務
使用該服務使用用戶控制
網站的用戶控件
這是我的服務we.config:
<system.serviceModel>
<services>
<service name="AttachmentService" behaviorConfiguration="">
<endpoint name="DefaultEndPoint" address="http://localhost:54893/AttachmentService.svc"
behaviorConfiguration="EndpointBehaviors"
binding="wsHttpBinding" bindingName="AttachmentBinding"
contract="AttachmentsService.IAttachmentService"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpointBehaviors">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="AttachmentBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="00:01:00" sendTimeout="00:01:00"
textEncoding="utf-8" openTimeout="00:01:00">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
這裏是我的用戶控制的app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAttachmentService" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:54893/AttachmentService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAttachmentService"
contract="AttachmentsService.IAttachmentService" name="BasicHttpBinding_IAttachmentService" />
</client>
</system.serviceModel>
</configuration>
這裏是我的UI的web.config:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" />
</requestFiltering>
</security>
<serverRuntime enabled="true" uploadReadAheadSize="2147483647" maxRequestEntityAllowed="2147483647" />
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAttachmentService" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:54893/AttachmentService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAttachmentService"
contract="AttachmentsService.IAttachmentService" name="BasicHttpBinding_IAttachmentService" />
</client>
</system.serviceModel>
這裏是我是如何實現的引用調用:
public void UploadFile(string serviceUrl,decimal maxFileSize, AttachmentFileParams fileParams, Stream file)
{
AttachmentServiceClient client = null;
try
{
if (file.Length > (maxFileSize * 1024)) //maxFileSize is defined in KB and file.Length is in Bytes
return Serialization.ConvertToJson(new { IsError = true, ErrorMessage = Constants.Messages.MaxFileSizeExceeded + maxFileSize });
string requestUrl = string.Format("{0}/UploadFile", serviceUrl);
string jsonFile = Serialization.ConvertToJson(fileParams);
byte[] jsonFileBytes = Encoding.UTF8.GetBytes(jsonFile);
byte[] len = BitConverter.GetBytes(jsonFileBytes.Length);
using (client = new AttachmentServiceClient())
{
client.UploadFile(file);
}
}
catch (Exception ex)
{ }
finally
{
try
{
if (client.State != System.ServiceModel.CommunicationState.Closed)
client.Close();
}
catch
{
client.Abort();
}
}
}
有什麼我失蹤了嗎?
可能這會幫助你。 http://stackoverflow.com/a/24932150/1660178 –
你需要在你的綁定中使用'transfermode'我想。關注https://msdn.microsoft.com/en-us/library/ms789010(v=vs。 110).aspx –