如何配置 SimpleInjector 以解决 LogMiddleware 的 Invoke 方法依赖关系,例如 IMessageService ?
据我所知,Asp.net 核心使用 HttpContext.RequestServices (IServiceProvider) 来解决依赖关系,我将 SimpleInjector 容器设置为 HttpContext.RequestServices 属性但没有用。我想动态更改 ServiceProvider 因为每个租户都应该有一个容器。
public class LogMiddleware
{
RequestDelegate next;
private readonly ILogger log;
public LogMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
this.next = next;
this.log = loggerFactory.CreateLogger<LogMiddleware>();
}
public async Task Invoke(HttpContext context, IMessageService messageService)
{
await context.Response.WriteAsync(
messageService.Format(context.Request.Host.Value));
}
}
public interface IMessageService
{
string Format(string message);
}
public class DefaultMessageService : IMessageService
{
public string Format(string message)
{
return "Path:" + message;
}
}