上传base64编码的字符串时,自托管服务报告413

时间:2015-03-20 15:21:56

标签: c#

我有一个自托管的Rest服务,用户报告在上传大图片时收到413错误。它适用于较小的字符串

图像以base64编码的字符串形式发送。

我觉得这与我需要从我读过的其他一些线程增加的默认限制有关。

但我一直无法弄清楚我需要添加配置的地方?

我是否需要将其添加到App.config?

这是我目前的App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="Service.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        <section name="HttpApiService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>
<userSettings>
<Service.Properties.Settings>
  <setting name="URL" serializeAs="String">
    <value>https://localhost:8083/</value>
  </setting>
</Service.Properties.Settings>
<HttpApiService.Properties.Settings>
  <setting name="URL" serializeAs="String">
    <value>https://localhost:8083/</value>
  </setting>
</HttpApiService.Properties.Settings>
</userSettings>
</configuration>

我觉得这有点像:

<system.serviceModel>
<webHttpBinding>
  <binding
    maxBufferPoolSize="2147483647"
    maxReceivedMessageSize="2147483647"
    maxBufferSize="2147483647" transferMode="Streamed">
  </binding>
</webHttpBinding>

但无论我在哪里尝试将该部分添加到我的app.config中,都会收到错误。

有人能告诉我如何在自托管服务中解决这个413问题吗?

1 个答案:

答案 0 :(得分:1)

我相信这就是答案......

_config.MaxReceivedMessageSize = 5242880; // 5mb
_config.MaxBufferSize = 5242880; // 5mb

以下内容:

namespace HttpApiService
{
public partial class HttpApiService : ServiceBase
{
    private HttpSelfHostServer _server;
    //private readonly HttpSelfHostConfiguration _config; // http
    private readonly MyHttpsSelfHostConfiguration _config; // https
    public string ServiceAddress = Settings.Default.URL;

    public HttpApiService()
    {
        InitializeComponent();

        _config = new MyHttpsSelfHostConfiguration(ServiceAddress);
        _config.MapHttpAttributeRoutes();
        _config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional });

        // added these to solve the upload size problem
        _config.MaxReceivedMessageSize = 5242880; // 5mb
        _config.MaxBufferSize = 5242880; // 5mb

    }

    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("HttpApiService started.");
        _server = new HttpSelfHostServer(_config);
        _server.OpenAsync();
    }

    protected override void OnStop()
    {
        EventLog.WriteEntry("HttpApiService stopped.");
        _server.CloseAsync().Wait();
        _server.Dispose();
    }

    class MyHttpsSelfHostConfiguration : HttpSelfHostConfiguration
    {
        public MyHttpsSelfHostConfiguration(string baseAddress) : base(baseAddress) { }
        public MyHttpsSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { }
        protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
        {
            httpBinding.Security.Mode = HttpBindingSecurityMode.Transport;
            //Console.WriteLine("https is on");
            return base.OnConfigureBinding(httpBinding);
        }
    }
}

}