Nancy OnError 不会接受 Response 对象?

Nancy OnError will not accept a Response object?

Nancy 文档似乎说 Pipelines.OnError 应该 return null - 而不是 BeforeResponse 允许 null 和 [=15] =]对象。

Whosebug 上的所有示例 like this one 和此处的许多代码示例显示 Response 在 OnError 中被 return 编辑,就像在 BeforeRequest 中一样。

当我尝试 return Pipelines.OnError 的 HTTPStatus 字符串时,一切正常!

但是当我尝试 return 响应时,出现编译器错误: 运算符“+=”不能应用于 'Nancy.ErrorPipeline' 和 'lambda expression'

类型的操作数

我几乎完全模拟了 Nancy 示例中的代码,除了我的是 TinyIocContainer 而示例使用的是 StructureMap 容器和 StructureMap 派生的引导程序

这是我的代码:

    const string errKey = "My proj error";
    const string creationProblem = "Message creation (HTTP-POST)";
    const string retrievalProblem = "Message retrieval (HTTP-GET)";

    public void Initialize(IPipelines pipelines)
    {
        string jsonContentType = "application/json";
        byte[] jsonFailedCreate = toJsonByteArray(creationProblem);
        byte[] jsonFailedRetrieve = toJsonByteArray(retrievalProblem);

        Response responseFailedCreate = new Response
        {
            StatusCode = HttpStatusCode.NotModified,
            ContentType = jsonContentType,
            Contents = (stream) => 
                stream.Write(jsonFailedCreate, 0, jsonFailedCreate.Length)
        };

        Response responseFailedRetrieve = new Response
        {
            StatusCode = HttpStatusCode.NotFound,
            ContentType = jsonContentType, 
            Contents = (stream) => 
                stream.Write(jsonFailedRetrieve, 0, jsonFailedRetrieve.Length)
        };

        // POST - error in Create call
        pipelines.OnError += (context, exception) =>
            {
                // POST - error during Create call
                if (context.Request.Method == "POST")
                    return responsefailedCreate;

                // GET - error during Retrieve call
                else if (context.Request.Method == "GET")
                    return responseFailedRetrieve;

                // All other cases - not supported
                else
                    return HttpStatusCode.InternalServerError;
            };
    }


    private byte[] toJsonByteArray(string plainString)
    {
        string jsonString = new JObject { { errKey, plainString } }.ToString();
        byte[] result = Encoding.UTF8.GetBytes(jsonString);
        return result;
    }

我遇到了同样的问题,我找到了解决问题的好方法:http://paulstovell.com/blog/consistent-error-handling-with-nancy

你应该覆盖引导程序上的 RequestStartup,这里是我的测试代码:

    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
    {           
        pipelines.OnError.AddItemToEndOfPipeline((ctx, ex) =>
            {                                       
                DefaultJsonSerializer serializer = new DefaultJsonSerializer();
                Response error = new JsonResponse(ex.Message,serializer);
                error.StatusCode = HttpStatusCode.InternalServerError;
                return error;
            });                 

        base.RequestStartup(container, pipelines, context);
    }