我有一个非常简单的 NancyFX 模块,我只是想将 API 调用的结果回显给发送者。
我正在使用一个外观,它将传入的 XML 转换为 JSON,然后再将其传递给 Nancy 端点。这个外观正确地将内容更改为 JSON,因为我可以使用 api 的 echo 服务对其进行测试并可以看到响应。
但是,因为外观移除了 content-length 标头并将传输编码设置为分块,所以我的 Nancy 模块中的 Request.Body 始终为空。
是否需要配置才能在 NancyFX 中启用对分块编码的支持?
我目前在 IIS 7 上托管,但也可以访问 IIS 8。
我可以看到使用 OWIN 托管可以使用 HostConfiguration 启用分块传输,但由于其他因素,我无法使用 OWIN 托管并依赖 IIS 托管。
我使用以下命令在 IIS 上启用了分块传输:
appcmd set config /section:asp /enableChunkedEncoding:True
我的 web.config 目前是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpHandlers>
<add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
</httpHandlers>
<httpRuntime targetFramework="4.5.1" />
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
<system.webServer>
<modules>
<remove name="WebDavModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
该模块本身非常简单,包括:
Post["/"] = parameters =>
{
var traceRef = Guid.NewGuid();
var body = this.Request.Body.AsString();
Logger.Trace("Trace ref: {0}, request inbound.", traceRef);
Logger.Trace(body);
AuthRequest auth = new AuthRequest();
try
{
auth = this.Bind<AuthRequest>();
}
catch (Exception ex)
{
Logger.Error("Trace ref: {0}, error: {1}. Exception: {2}", traceRef, ex.Message, ex);
}
var responseObject = new
{
this.Request.Headers,
this.Request.Query,
this.Request.Form,
this.Request.Method,
this.Request.Url,
this.Request.Path,
auth
};
return Response.AsJson(responseObject);
};