WCF - 大文件上传 - (413) 请求实体太大
WCF - large files upload - (413) Request Entity Too Large
有人可以看看我的新代码吗?我已经被这个错误困扰了 3 天,它快把我逼疯了...
我正在尝试上传 WCF 文件,但没有上传大文件
我收到此错误:"The remote server returned an unexpected response: (413) Request Entity Too Large."
我的项目由 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();
}
}
}
有什么我遗漏的吗?
我在使用 IIS 服务器上传大文件时遇到了同样的问题。
我发现有两个地方你需要修改你的web.config
<system.web>
<httpRuntime maxRequestLength="50000"/>
</system.web>
和
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="50000000" /> <!-- Allow files of upto 50,000,000 bytes (about 50Mb) to be uploaded -->
</requestFiltering>
</security>
</system.webServer>
希望这对您有所帮助。
这里有更多详细信息:
Upload large files
好吧,我终于解决了这个问题,这是对我有用的配置文件:
WCF 服务web.config 文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<!-- The name of the service -->
<service name="WcfService1.Service1">
<!--you can leave the address blank or specify your end point URI-->
<endpoint binding="webHttpBinding" bindingConfiguration="higherMessageSize" contract="WcfService1.IService1" behaviorConfiguration="Web" />
</service>
</services>
<bindings>
<webHttpBinding>
<!-- configure the maxReceivedMessageSize value to suit the max size of the request (in bytes) you want the service to receive-->
<binding name="higherMessageSize" transferMode="Streamed" maxReceivedMessageSize="2147483647" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />-->
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
客户端app.config/web.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="2147483647" />
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:59540/Service1.svc" behaviorConfiguration="webhttp"
binding="webHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="webhttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我希望它能节省其他人解决问题的时间:-)
有人可以看看我的新代码吗?我已经被这个错误困扰了 3 天,它快把我逼疯了...
我正在尝试上传 WCF 文件,但没有上传大文件
我收到此错误:"The remote server returned an unexpected response: (413) Request Entity Too Large."
我的项目由 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();
}
}
}
有什么我遗漏的吗?
我在使用 IIS 服务器上传大文件时遇到了同样的问题。
我发现有两个地方你需要修改你的web.config
<system.web>
<httpRuntime maxRequestLength="50000"/>
</system.web>
和
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="50000000" /> <!-- Allow files of upto 50,000,000 bytes (about 50Mb) to be uploaded -->
</requestFiltering>
</security>
</system.webServer>
希望这对您有所帮助。
这里有更多详细信息: Upload large files
好吧,我终于解决了这个问题,这是对我有用的配置文件:
WCF 服务web.config 文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<!-- The name of the service -->
<service name="WcfService1.Service1">
<!--you can leave the address blank or specify your end point URI-->
<endpoint binding="webHttpBinding" bindingConfiguration="higherMessageSize" contract="WcfService1.IService1" behaviorConfiguration="Web" />
</service>
</services>
<bindings>
<webHttpBinding>
<!-- configure the maxReceivedMessageSize value to suit the max size of the request (in bytes) you want the service to receive-->
<binding name="higherMessageSize" transferMode="Streamed" maxReceivedMessageSize="2147483647" />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />-->
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
客户端app.config/web.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="2147483647" />
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:59540/Service1.svc" behaviorConfiguration="webhttp"
binding="webHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="webhttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我希望它能节省其他人解决问题的时间:-)