ASP.NET Core Disable response decompression

我的未来我决定 提交于 2020-04-18 09:43:48

问题


I need make response compression disabled. I have .NET 4.5 version of web.config but I need in .NET CORE 2.x now.

<customBinding>
    <binding name="BasicHttpBinding_Service">
        <textMessageEncoding messageVersion="Soap11" />
        <httpsTransport decompressionEnabled="false" />
    </binding>
</customBinding>

I have web service which is not support compressed response and I am still get error: System.ServiceModel.ProtocolException: 'The content type application/x-gzip of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 653 bytes of the response were: '?'

Thank you very much


回答1:


ASP.NET Core has documentation on compression. As you can see, ASP.NET Core no longer uses web.config. If you're hosting your ASP.NET Core app in IIS, then some web.config settings are still used, but only those relevant to generic IIS hosting, nothing application specific (dynamic compression is an IIS feature, so that setting is still relevant, but other web.config settings that used to be relevent in ASP.NET apps are no longer used in ASP.NET Core). Unfortunately I can't find documentation at the moment as to which web.config settings are still relevant.

Anyway, ASP.NET Core by default uses the Kestrel web server, which is configured in code. Making a small modification to the sample in the documentation I linked above, I beleive this might work:

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression(options =>
    {
        var gzip = options.Providers.OfType<GzipCompressionProvider>().FirstOrDefault();
        if (gzip != null)
        {
            options.Providers.Remove(gzip);
        }
    });
}

But it depends where the compression is coming from. If you have a reverse proxy between your ASP.NET Core app and the internet (for example IIS, but maybe nginx, haproxy, etc) they might be doing compression, so you need to understand your application architecture and turn it off at the right place.



来源:https://stackoverflow.com/questions/53559038/asp-net-core-disable-response-decompression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!