如何将 Service Fabric 服务上下文注入 asp.net 核心中间件?
How to inject Service Fabric service context into asp.net core middleware?
我有一个 Service Fabric asp.net 核心无状态服务,它实现了自定义中间件。在那个中间件中,我需要访问我的服务实例。我将如何使用 asp.net 核心的内置 DI/IoC 系统注入它?
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
// ** need access to service instance here **
return _next(httpContext);
}
}
有人提到在 Apr 20, 2017 Q&A #11 [45:30] 中与 Service Fabric 团队一起使用 TinyIoC in Web Api 2 完成此操作。以及当前推荐的方法是使用 asp.net 核心。
任何帮助或示例将不胜感激!
通过构造函数的依赖注入适用于中间件 类 以及其他。只需向中间件构造函数添加额外的参数
public MyMiddleware(RequestDelegate next, IMyService myService)
{
_next = next;
...
}
但是你也可以直接添加依赖到Invoke
方法
Documentation: Because middleware is constructed at app startup, not per-request, scoped lifetime services used by middleware constructors are not shared with other dependency-injected types during each request. If you must share a scoped service between your middleware and other types, add these services to the Invoke method's signature. The Invoke
method can accept additional parameters that are populated by dependency injection.
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext, IMyScopedService svc)
{
svc.MyProperty = 1000;
await _next(httpContext);
}
}
在创建 ServiceInstanceListener
的 asp.net 核心无状态服务中,您可以像这样注入上下文:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
logger.LogStatelessServiceStartedListening<WebApi>(url);
return new WebHostBuilder().UseWebListener()
.ConfigureServices(
services => services
.AddSingleton(serviceContext) // HERE IT GOES!
.AddSingleton(logger)
.AddTransient<IServiceRemoting, ServiceRemoting>())
.UseContentRoot(Directory.GetCurrentDirectory())
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
};
}
你的中间件可以这样使用:
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext, StatelessServiceContext serviceContext)
{
// ** need access to service instance here **
return _next(httpContext);
}
}
有关完整示例,请查看此存储库:https://github.com/DeHeerSoftware/Azure-Service-Fabric-Logging-And-Monitoring
您的兴趣点:
我有一个 Service Fabric asp.net 核心无状态服务,它实现了自定义中间件。在那个中间件中,我需要访问我的服务实例。我将如何使用 asp.net 核心的内置 DI/IoC 系统注入它?
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
// ** need access to service instance here **
return _next(httpContext);
}
}
有人提到在 Apr 20, 2017 Q&A #11 [45:30] 中与 Service Fabric 团队一起使用 TinyIoC in Web Api 2 完成此操作。以及当前推荐的方法是使用 asp.net 核心。
任何帮助或示例将不胜感激!
通过构造函数的依赖注入适用于中间件 类 以及其他。只需向中间件构造函数添加额外的参数
public MyMiddleware(RequestDelegate next, IMyService myService)
{
_next = next;
...
}
但是你也可以直接添加依赖到Invoke
方法
Documentation: Because middleware is constructed at app startup, not per-request, scoped lifetime services used by middleware constructors are not shared with other dependency-injected types during each request. If you must share a scoped service between your middleware and other types, add these services to the Invoke method's signature. The
Invoke
method can accept additional parameters that are populated by dependency injection.
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext, IMyScopedService svc)
{
svc.MyProperty = 1000;
await _next(httpContext);
}
}
在创建 ServiceInstanceListener
的 asp.net 核心无状态服务中,您可以像这样注入上下文:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
logger.LogStatelessServiceStartedListening<WebApi>(url);
return new WebHostBuilder().UseWebListener()
.ConfigureServices(
services => services
.AddSingleton(serviceContext) // HERE IT GOES!
.AddSingleton(logger)
.AddTransient<IServiceRemoting, ServiceRemoting>())
.UseContentRoot(Directory.GetCurrentDirectory())
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
};
}
你的中间件可以这样使用:
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext, StatelessServiceContext serviceContext)
{
// ** need access to service instance here **
return _next(httpContext);
}
}
有关完整示例,请查看此存储库:https://github.com/DeHeerSoftware/Azure-Service-Fabric-Logging-And-Monitoring
您的兴趣点: