我需要一个服务来处理来自多个不同服务的传入 webhook 负载,并将它们传递给正确的实现进行处理。
我不确定我是否/应该在何处实例化我的协调器服务,它应该与 IHostBuilder 一起运行应用程序的生命周期。我应该创建一个 Thread
并在那里启动我的协调器吗?
服务接口
public interface IService
{
event WorkCreated(object processedPayload);
void ProcessWebhook(string payload);
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IService, Service1>();
services.AddSingleton<IService, Service2>();
services.AddSingleton<IService, Service3>();
}
协调员服务
public Class Coordinator
{
public Coordinator(IList<IService> services
{
foreach service
service.WorkCreated += eventHandler;
}
public eventHandler()
{
...
}
}
控制器
public class WebhookController : ControllerBase
{
public WebhookController(IList<IService> services
{
...
}
public ActionResult Webhook(string payload)
{
// Check which implementation to use by the header
var referrer = this.Request.Headers["Referer"];
if (referrer == "service1.com")
{
var serviceToUse = services.Where(s => s.GetType() == typeof(Service1));
serviceToUse.ProcessWebhook(payload);
}
return Ok();
}