。Net Core Alpine容器中使用ServiceStack解压缩请求

问题描述 投票:1回答:1

我正在构建一个容器化微服务,该服务使用在ASPNET Core Alpine docker映像上与.Net Core一起运行的ServiceStack。我希望能够在请求正文中接收包含Gzipped JSON的压缩请求,并在请求到达ServiceStack之前对其进行解压缩,以便基于解压缩的JSON数据填充请求DTO。

到目前为止,我已经尝试滚动自己的中间件来做到这一点,但是我的请求DTO仍然填充了Null。我也尝试使用Anemonis.AspNetCore.RequestDecompression中间件,结果相同。我现在想知道在ServiceStack接收请求之前是否不调用中间件,或者甚至根本不调用中间件。

使用Anemonis中间件,我的Startup.cs就这样初始化中间件:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //...

    app.UseRequestDecompression();

    app.UseServiceStack(new AppHost
    {
        AppSettings = new NetCoreAppSettings(Configuration),
    });

    //...
}

使用ConfigureServices中提供的解压缩:

public new void ConfigureServices(IServiceCollection services)
{
    //...

    services.AddRequestDecompression(o =>
    {
        o.Providers.Add<DeflateDecompressionProvider>();
        o.Providers.Add<GzipDecompressionProvider>();
        o.Providers.Add<BrotliDecompressionProvider>();
    });

    //...
}

还有进一步的详细说明,ServiceStack服务模型和接口照常:

[Route("/sample/full", "POST")]
public class SampleFull : IReturn<SampleResponse>
{
    public long code { get; set; }
    public SampleData[] data { get; set; }
}

public class SampleData
{
    public string field1 { get; set; }
    public string field2 { get; set; }
}

public class SampleService : Service
{
    public object Post(SampleFull request)
    {
        try
        {
            // Do some processing

            return new SampleResponse()
            {
                // Response details
            }
        }
        catch (Exception ex)
        {
            return new SampleResponse()
            {
                // Error details
            };
        }
    }
}

使用邮递员进行测试,Content-Encoding = gzip,并使用压缩文件作为请求正文,当调用Post(SampleFull request)时,request会填充codedata的空值。

有人能使这个工作正常吗?我现在在想,我可能会在ASPNET Core Alpine容器中缺少一个库/程序包。

asp.net-core .net-core servicestack alpine
1个回答
0
投票

我终于找到了问题,回答我自己的问题。事实证明,默认情况下,当您选择发送二进制文件作为请求正文时,Postman将发送Content-Type标头,其值为application / octet-stream。默认情况下,此标头是隐藏的,您无法覆盖它,但是可以禁用它,并添加自己的Content-Type标头,其值为application / json,这将允许ServiceStack正确填充其请求DTO。解决。

© www.soinside.com 2019 - 2024. All rights reserved.