从web.config激活DelegatingHandler

时间:2017-02-10 13:20:02

标签: asp.net-web-api2

是否有办法让DelegatingHandler仅在生产环境中处于活动状态?比如从web.config中将其添加到配置中,而不是将其作为Application_Start()添加到GlobalConfiguration.Configuration.MessageHandlers.Add(....上?

1 个答案:

答案 0 :(得分:0)

不需要将它添加到消息处理程序中,但您可以做的是创建处理程序以从配置文件中获取一个设置,该设置将指示它是否应该执行其设计目的或仅让请求通过

伪代码

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {

    bool active = //...get configuration value;

    if(active) {
        //...perform some action
    }

    var response = await base.SendAsync(request, cancellationToken);

    if(active) {
        //...perform some action
    }

    return response
}