WCF RestFul“413请求实体太大”

时间:2014-06-04 21:37:21

标签: c# wcf rest

您好我正在使用WCF RestFull服务,使用JsonNet来序列化我的对象 我有这个方法: User对象具有类似于

的属性类型byte []
public class User
{
   public int Id {get;set;}
   public string UserName {get;set}
   public byte[] Photo {get;set;}
}

但是,我发布一张超过41 Kb的Photo大小的POST,得到了HttpStatusCode" 413请求实体太大"

[WebInvoke(Method = "PUT",
         RequestFormat = WebMessageFormat.Json,
         ResponseFormat = WebMessageFormat.Json,
         BodyStyle = WebMessageBodyStyle.Wrapped,
         UriTemplate = "/user/{id}/")]
        public Stream EditUser(string id, Stream input)
        {
            try
            {
                string json = input.ConvertToString();
                User usr = json.FromJSON<User>();
                usr.Update();

                return null;
            }             
            catch (Exception ex)
            {  
                SetInternalServerErrorResponse();
                return GetResponseError(ex);
            }
        }

这是我的webconfig

<bindings>
              <basicHttpBinding>
                  <binding name="" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
                      <readerQuotas maxDepth="35" maxStringContentLength="65536000" maxArrayLength="65536000" maxBytesPerRead="65536000" />
                      <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                  </binding> 
              </basicHttpBinding>
              <webHttpBinding>
                  <binding name="svcBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">               
                      <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                      <security mode="None">
                      </security>
                  </binding>
              </webHttpBinding>
  </bindings>
编辑:我在这里找到了解决方案:

maxReceivedMessageSize not fixing 413: Request Entity Too Large

该帖子的第一个回复。

添加bindingConfiguration proprety。

  <service  behaviorConfiguration="serviceBehavior" name="Aloes.Services.Service">
     <endpoint  address="" behaviorConfiguration="web"  binding="webHttpBinding" bindingConfiguration="svcBinding"
      contract="Aloes.Services.IService" />
    </service>

1 个答案:

答案 0 :(得分:3)

要克服此错误,您需要在配置文件(app.config或web.config)中配置WebHttpBinding:

<system.servicemodel>  
    <bindings>  
        <webHttpBinding>  
            <binding maxbufferpoolsize="2147483647"
                     maxreceivedmessagesize="2147483647"
                     maxbuffersize="2147483647"> 
            </binding>  
        </webHttpBinding>  
    </bindings>  
</system.servicemodel>

您需要设置maxbufferpoolsizemaxreceivedmessagesizemaxbuffersize