我正在构建一个容器化微服务,该服务使用在 ASPNET Core Alpine docker 镜像上与 .Net Core 一起运行的 ServiceStack。我希望能够接收请求正文中包含压缩 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 服务模型和接口(interface)与往常一样:
[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
};
}
}
}
使用Postman进行测试,Content-Encoding = gzip,以gzip压缩后的文件作为请求体,当调用Post(SampleFull request)
时,request
填充为code
和 data
都为 null 值。
有人能够让它工作吗?我现在认为我可能缺少 ASPNET Core Alpine 容器中的库/包。
最佳答案
回答我自己的问题,因为我终于找到了问题。事实证明,默认情况下,当您选择发送二进制文件作为请求正文时,Postman 将发送值为 application/octet-stream 的 Content-Type header 。默认情况下,此 header 是隐藏的,您无法覆盖它,但您可以禁用它并添加自己的 Content-Type header ,其值为 application/json,然后允许 ServiceStack 正确填充其请求 DTO。已解决。
关于asp.net-core - 在 .Net Core Alpine 容器中使用 ServiceStack 解压缩请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61725179/