WCF RestFull“413 请求实体太大”

jcm*_*dan 2 c# rest wcf

嗨,我使用 WCF RestFull 服务工作,使用 JsonNet 序列化我的对象我有这个方法:用户对象有一个属性类型 byte[] 像这样

public class User
{
   public int Id {get;set;}
   public string UserName {get;set}
   public byte[] Photo {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

但是我发了一个帖子,照片的大小超过 41 Kb 得到了 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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

这是我的网络配置

<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>
Run Code Online (Sandbox Code Playgroud)

编辑:我在这里找到解决方案:

maxReceivedMessageSize 未修复 413:请求实体太大

帖子的第一个回复。

添加 bindingConfiguration 属性。

  <service  behaviorConfiguration="serviceBehavior" name="Aloes.Services.Service">
     <endpoint  address="" behaviorConfiguration="web"  binding="webHttpBinding" bindingConfiguration="svcBinding"
      contract="Aloes.Services.IService" />
    </service>
Run Code Online (Sandbox Code Playgroud)

小智 5

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

<system.servicemodel>  
    <bindings>  
        <webHttpBinding>  
            <binding maxbufferpoolsize="2147483647"
                     maxreceivedmessagesize="2147483647"
                     maxbuffersize="2147483647"> 
            </binding>  
        </webHttpBinding>  
    </bindings>  
</system.servicemodel>
Run Code Online (Sandbox Code Playgroud)

您需要设置maxbufferpoolsize,maxreceivedmessagesizemaxbuffersize