gpt4 book ai didi

c# - base.SendAsync - 对称执行是如何完成的?

转载 作者:行者123 更新时间:2023-11-30 19:58:34 28 4
gpt4 key购买 nike

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

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

enter image description here

因此,例如:如果我附加 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());

结果是:

enter image description here

如你所见,就像我说的和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) 来自每个方法。

类似的东西:

enter image description here

所以我查看了 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com