我正在尝试使用Simple Injector创建一个.net核心WebApi服务。在我的Startup.cs我有这个:使用Simple Injector的asp.net Core Web API服务的正确DefaultScopedLifestyle是什么?
public Startup(IHostingEnvironment env)
{
// Setup configuration stuff
container = new Container();
container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle(); // This is as specified in the docs.
}
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddOptions();
services.AddSingleton<IControllerActivator>(
new SimpleInjectorControllerActivator(this.container));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSimpleInjectorAspNetRequestScoping(this.container);
InitializeContainer(app); // This registers stuff
this.container.Verify(); // This blows up.
app.UseMvc();
}
private void InitializeContainer(IApplicationBuilder app)
{
// Add application presentation components:
this.container.RegisterMvcControllers(app);
this.container.RegisterSingleton(new Log4NetAdapter(LogManager.GetLogger("Services.Api").Logger));
// Register other stuff here.
}
这编译得很好。但是,当我尝试启动我上container.Verify()
步以下错误消息的服务:
{“的配置无效以下诊断警告报道: - [一次性暂态] MessageController被注册为短暂的,但实现IDisposable 请参阅有关警告的详细信息的错误性质请参阅https://simpleinjector.org/diagnostics如何解决问题,如何抑制各个警告“}
- 。[一次性暂态] MessageController注册为短暂的,但实现了IDisposable。 该配置无效。报告了以下诊断警告: 有关警告的详细信息,请参阅Error属性。请参阅https://simpleinjector.org/diagnostics如何解决问题以及如何抑制个别警告。
我试过,包括必要的NuGet包使用WebApiRequestLifestyle
,但 看来,只有用标准的.NET项目有效;不是.net核心。
我在文档中找到this page。我能忽略我的具体控制器警告运行的服务:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSimpleInjectorAspNetRequestScoping(this.container);
InitializeContainer(app); // This registers stuff
var registration = container.GetRegistration(typeof(MessageController)).Registration;
registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "This is a Web API controller. Disposal should be handled by the system.");
this.container.Verify(); // Now this works
app.UseMvc();
}
但问题是,现在我必须去和抑制每增加控制器警告。这不应该由默认生活方式来处理吗?
您使用的是哪个版本的SI和集成包? – Steven