我已經使用SimpleInjector按照與here相同的方式設置了我的依賴注入。不幸的是,容器調用對象的RegisterMvcViewComponents
拋出異常:如何在.NET Core中註冊IViewComponentDescriptorProvider?
爲 型無此項服務「Microsoft.AspNetCore.Mvc.ViewComponents.IViewComponentDescriptorProvider」 已註冊。
容器是否有註冊提供者的相應方法?還是應該以其他方式完成?
的代碼:
public class Startup
{
private Container _container = new Container();
public void ConfigureServices(IServiceCollection services)
{
services.InitializeTestData();
services.AddMvcCore();
services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(_container));
services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(_container));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSimpleInjectorAspNetRequestScoping(_container);
_container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();
InitializeContainer(app);
_container.Verify();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Default",
template: "{controller=Home}/{action=Index}/{id?}"
);
});
}
private void InitializeContainer(IApplicationBuilder app)
{
_container.RegisterMvcControllers(app);
_container.RegisterMvcViewComponents(app);
_container.Register<IDbContext>(() => new DbContext("GuitarProject"));
_container.Register<IJournalEntryRepository, JournalEntryRepository>();
}
}
堆棧跟蹤(異常的類型是InvalidOperationException
):
在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(的IServiceProvider 提供商類型的serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](IServiceProvider provider)at SimpleInjector.SimpleInjectorAspNetCoreMvcIntegrationExtensions.RegisterMvcViewComponents(容器 容器,IApplicationBuilder applicationBuilder)在 ProjectX.Startup.InitializeContainer(IApplicationBuilder應用)
你可以發佈完整的堆棧跟蹤嗎? – Steven
@Steven堆棧跟蹤添加。 – Kapol