How to Intercept all Nancy requests

时间秒杀一切 提交于 2021-01-05 06:10:35

问题


I have seen this post: Nancy: how do I capture all requests irrespective of verb or path and followed along on the github article.

But it does not work. I have simply added a class in my project:

 public class MyBootstrapper : Nancy.DefaultNancyBootstrapper

But this class is never instantiated, and the github documentation does not discuss this in any detail.

What do I need to do to cause my bootstrapper to be used?


回答1:


I found it. There are two ways to add items to the pipeline. One by deriving a Bootstrap class, which failed for me. The other by implementing a class which honored the IApplicationStartup interface. That worked, and here is the code:

  public class BeforeAllRequests : IApplicationStartup
{
    public void Initialize(IPipelines pipelines)
    {
        pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx => {
            if (ctx != null)
            {
                Log.Debug("Request: " + ctx.Request.Url);
            }
            return null;
        });
    }
}



回答2:


This worked for me (4 years later, maybe the Wiki changed since then): Bootstrapper



来源:https://stackoverflow.com/questions/34823373/how-to-intercept-all-nancy-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!