English 中文(简体)
ASP. NET 万维网短信
原标题:ASP.NET Web API Message Handlers

Is it possible to control the execution order of custom message handlers?

例如,我可能希望伐木手先执行,因此我总是提出申请。

除了最后增加伐木手,我也看不到如何做到这一点。

config.MessageHandlers.Add(new CustomHandlerOne()); 
config.MessageHandlers.Add(new CustomHandlerTwo());
config.MessageHandlers.Add(new LoggingHandler());
最佳回答

您登记操作员的顺序决定他们何时被点名,但阿利奥斯塔德指出,他们以俄罗斯的代谢模式工作,因此,第一种方式也被称为最后一种。

在即将到来的道路和即将到来的自上而下的道路上以自下而上的方式援引注册的操作员。 这就是说,最后的条目首先要求发出一项请求信息,但最后被援引发出即将发出的答复信息。

问题回答

I am talking here based on the latest bits which are available on Codeplex ASP.NET Web Stack repo.

该命令由用户控制,没有任意命令。 让我解释:

Let s say we have two message handlers: MyMessageHandler and MyMessageHandler2. Assuming that we register them as below:

protected void Application_Start(object sender, EventArgs e) {

    RouteConfig.RegisterRoutes(GlobalConfiguration.Configuration.Routes);
    GlobalConfiguration.Configuration.MessageHandlers.Add(new MyMessageHandler());
    GlobalConfiguration.Configuration.MessageHandlers.Add(new MyMessageHandler2());
}

What you expect here is for the MyMessageHandler to run first and MyMessageHandler2 as second one, in other words FIFO.

如果我们看一看框架中的一环,我们将看到<条码>>><条码>方法>。 HttpServer instance isciting CreatePipeline methods of System.Net.Http.HttpClientFactory (以前称为<代码>) HttpPipelineFactory.Create methods as Ali indicated.)CreatePipeline 方法接受两个参数:HttpMessageHandlerIE数<DelegatingHandler>。 <代码>HttpServer.Initialize方法是通过<编码>System.Web.Http.Displer,作为最后<编码>。 HttpMessageHandler within the chain and HttpConfiguration. 信号Handlers for IEvidable<DelegatingHandler> para amount.

<代码>CreatePipeline内发生的情况 这种方法非常切合海事组织:

public static HttpMessageHandler CreatePipeline(HttpMessageHandler innerHandler, IEnumerable<DelegatingHandler> handlers)
{
    if (innerHandler == null)
    {
        throw Error.ArgumentNull("innerHandler");
    }

    if (handlers == null)
    {
        return innerHandler;
    }

    // Wire handlers up in reverse order starting with the inner handler
    HttpMessageHandler pipeline = innerHandler;
    IEnumerable<DelegatingHandler> reversedHandlers = handlers.Reverse();
    foreach (DelegatingHandler handler in reversedHandlers)
    {
        if (handler == null)
        {
            throw Error.Argument("handlers", Properties.Resources.DelegatingHandlerArrayContainsNullItem, typeof(DelegatingHandler).Name);
        }

        if (handler.InnerHandler != null)
        {
            throw Error.Argument("handlers", Properties.Resources.DelegatingHandlerArrayHasNonNullInnerHandler, typeof(DelegatingHandler).Name, "InnerHandler", handler.GetType().Name);
        }

        handler.InnerHandler = pipeline;
        pipeline = handler;
    }

    return pipeline;
}

如你所知,电文手令被推翻,。 Matryoshka doll 这里虽已建立,但需谨慎:确保<代码>。 HttpControllerDispader 是链条内最后的电传手。

至于两个问题,这实际上并不完全。 贺电手得双倍,而你提供的持续方法则会是这样。 你们有责任做到这一点。 如果你提供反馈(换言之,继续),你的信息处理员将被要求向客户提供你可以提供的回复信息。

例如,请假设以下两点是上文所述的信息处理器:

public class MyMessageHandler : DelegatingHandler {

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {

        //inspect request here

        return base.SendAsync(request, cancellationToken).ContinueWith(task => {

            //inspect the generated response
            var response = task.Result;

            return response;
        });
    }
}

这是另一个:

public class MyMessageHandler2 : DelegatingHandler {

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {

        //inspect request here

        return base.SendAsync(request, cancellationToken).ContinueWith(task => {

            //inspect the generated response
            var response = task.Result;

            return response;
        });
    }
}

随着我们的持续,我们的信息处理员将回到劳工组织命令的客户手中。 因此,<代码>内的延续方法 MyMessageHandler2将是背路上首先要援引的,是内的。 MyMessageHandler将是第二个。

No - AFAIK.

它是俄罗斯的多拉模式,在最后一次工作之前,有一位手在另一边。 内部栏目<代码> HttpPipelineFactory (在排放时,可以认为来源代码):

    public static HttpMessageHandler Create(IEnumerable<DelegatingHandler> handlers, HttpMessageHandler innerChannel)
    {
        if (innerChannel == null)
        {
            throw Error.ArgumentNull("innerChannel");
        }

        if (handlers == null)
        {
            return innerChannel;
        }

        // Wire handlers up
        HttpMessageHandler pipeline = innerChannel;
        foreach (DelegatingHandler handler in handlers)
        {
            if (handler == null)
            {
                throw Error.Argument("handlers", SRResources.DelegatingHandlerArrayContainsNullItem, typeof(DelegatingHandler).Name);
            }

            if (handler.InnerHandler != null)
            {
                throw Error.Argument("handlers", SRResources.DelegatingHandlerArrayHasNonNullInnerHandler, typeof(DelegatingHandler).Name, "InnerHandler", handler.GetType().Name);
            }

            handler.InnerHandler = pipeline;
            pipeline = handler;
        }

        return pipeline;
    }

因此,《守则》是一份名单,然后将其变为。 Russian Doll





相关问题
Download file using Web API call

I am using Web API to download a file. Let me first preface that this is new territory for me. My download function inside the Web API code will initiate a download if I go directly to the API s ...

Indian Railway Train Search API [closed]

Is there any API provided by Indian Railways to search its train network, time-tables etc. There are many sites out there which show time-table etc. I searched Google but couldn t find any info on Web ...

Freely Available Web Services/APIs [closed]

I m building some Silverlight applications in my spare time, and I wanted find some freely available web services or APIs that I can call. Any suggestions?

What s the simplest way to do authentication with a web API?

I ve got a web API that provides data to users without authentication (the website lets users post data, after they ve logged in using traditional cookies & sessions). Someone wants to develop an ...

ISBN -> bookdata Lookup to fill in a database

Ok, I wanting to database a small library. I ve only had limited experience with databases, and none with querying from a webserver. I m going to want to retrieve information like title, Publisher, ...

Does IMDB provide an API? [closed]

I recently found a movie organizer application which fetches its data from the IMDB database. Does IMDB provide an API for this, or any third party APIs available?

Google Alerts web API for Python?

Google web APIs have a Python library. Google Alerts has an email and an RSS feed. Does anyone know of an automated way to add Google Alerts through a web API in Python? That is, without clicking. ...

Finding Websites From Company Name

I ve got a list of 6,000 company names (along with their headquarters address) and I need to find the web address for each of them. I m considering using the Google Web API (obviously this will take a ...

热门标签