我正在嘗試使用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