In asp.net - using Message Handlers-我可以通过添加消息处理程序来自定义请求/响应。

因此,一个请求进入,通过多个消息处理程序,然后响应通过相同的处理程序返回(方向相反)。



因此,例如:如果我附加了2个消息处理程序:(是的,我知道,首选async / await,但这是从书中获得的)

public class CustomMessageHandler1 : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        Debug.WriteLine("CustomMessageHandler1 request invoked");
        return base.SendAsync(request, cancellationToken)
                   .ContinueWith(task =>
                                      {
                                      Debug.WriteLine("CustomMessageHandler1 response invoked");
                                      var response = task.Result;
                                      return response;
                                      });
    }
}


public class CustomMessageHandler2 : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        Debug.WriteLine("CustomMessageHandler2 request invoked");
        return base.SendAsync(request, cancellationToken)
                   .ContinueWith(task =>
                                      {
                                      Debug.WriteLine("CustomMessageHandler2 response invoked");
                                      var response = task.Result;
                                      return response;
                                 });
    }
}


我们不要忘记在global.asax中注册它们:

var config = GlobalConfiguration.Configuration;
config.MessageHandlers.Add(new CustomMessageHandler1());
config.MessageHandlers.Add(new CustomMessageHandler2());


结果是:



如您所见,就像我说的和like this article says:执行是对称的。

大。

但是后来我想到了自己-他们是如何对称执行的?

因此,我成功地使用延续成功创建了自己的对称执行演示:

void Main()
{
 Method1() ;
}

  public async  Task  Method1 ()
  {
   Console.WriteLine ("Method_1"); //alias to request
   await Method2();
   Console.WriteLine ("Finished Method_1");  //alias to response
  }


 public async Task  Method2 ()
 {
  Console.WriteLine ("Method_2");  //alias to request
  await Task.FromResult("...");//dummy
  Console.WriteLine ("Finished Method_2");  //alias to response

 }


结果确实是对称的:

Method_1
Method_2
Finished Method_2
Finished Method_1


但是在我的代码Method1中叫做Method2,这就是它起作用的原因!

但是在上面的第一个代码中-它们不会互相调用!就像是从每个方法仅调用第一部分(在ContinueWith之前),然后从每个方法运行第二部分(在ContinueWith之后)一样。

就像是 :



所以我看了reference source for base.Sendasync :但是找不到base.Sendasync如何进行对称执行



base.Sendasync如何在没有一个方法调用另一个方法的情况下进行对称执行?

最佳答案

这是适合您控制台的Web API管道。

abstract class BaseHandler // HttpHandler
{
    public abstract Task MyMethodAsync();
}

abstract class Handler : BaseHandler // MessageHandler
{
    public Handler InnerHandler { get; set; }

    public override Task MyMethodAsync()
    {
        if (this.InnerHandler != null)
            return this.InnerHandler.MyMethodAsync();
        else
            return Task.FromResult<object>(null);
    }
}

class Handler1 : Handler
{
    public override async Task MyMethodAsync()
    {
        Console.WriteLine("Method_1"); //alias to request
        await base.MyMethodAsync();
        Console.WriteLine("Finished Method_1");  //alias to response
    }
}

class Handler2 : Handler
{
    public override async Task MyMethodAsync()
    {
        Console.WriteLine("Method_2"); //alias to request
        await base.MyMethodAsync();
        Console.WriteLine("Finished Method_2");  //alias to response
    }
}

class LastHandler : Handler
{
    public override async Task MyMethodAsync()
    {
        // Does nothing
        await base.MyMethodAsync();
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Handler> handlers = new List<Handler>();
        // You do this when you add the handler to config
        handlers.Add(new Handler1());
        handlers.Add(new Handler2());

        // This part is done by HttpClientFactory
        Handler pipeline = new LastHandler();

        handlers.Reverse();
        foreach (var handler in handlers)
        {
            handler.InnerHandler = pipeline;
            pipeline = handler;
        }

        pipeline.MyMethodAsync().Wait();
    }
}

关于c# - base.SendAsync-如何对称执行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26439913/

10-12 13:10