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インターを実装 が警告に関する詳細な情報については、Errorプロパティを参照してください問題を解決する方法と、個別の警告を抑制する方法https://simpleinjector.org/diagnostics参照してください "}
- 。。。[使い捨て過渡コンポーネント] MessageControllerは一時的なものとして登録され、 IDisposableを実装しています。 設定が無効です。次の診断警告が報告されました。 警告の詳細については、Errorプロパティを参照してください。問題を解決する方法と個々の警告を抑制する方法については、https://simpleinjector.org/diagnosticsを参照してください。
私はWebApiRequestLifestyle
を使用するために必要なnugetパッケージを含むしようとしたが、それ はそれが標準の.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