Transfer-Encoding 分块时的空请求 body - NancyFX

Empty request body when Transfer-Encoding is Chunked - NancyFX

我有一个非常简单的 NancyFX 模块,我只是想将 API 回调的结果回显给发件人。

我正在使用一个 facade,它在将传入的 XML 传递给 Nancy 端点之前将其转换为 JSON。此外观将内容正确更改为 JSON,因为我可以使用 api 的回显服务对其进行测试,并且可以看到响应。

但是,因为 facade 删除了 content-length header 并将 transfer-encoding 设置为分块,所以 Request.Body 在我的 Nancy 模块中总是空的。

是否需要配置才能在 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);
    };

我读这篇文章时的第一个想法是传输编码仅用于响应而不是请求。查看 list of HTTP header fields, Transfer-Encoding is only listed under Response Fields. But the spec 并没有仅提及发送者和接收者的请求或响应。现在我不太确定了。

无论如何,ASP.NET hosting code explicitly excludes the body if the content length is 0, but the self-hosting code似乎没有相同的限制。我不确定这种差异是否是故意的。您可以删除检查内容长度的 if 语句并将 PR 发送给 Nancy 团队。看看他们带回来了什么。