该问题通常是由于缺少依赖注入或未正确配置依赖注入容器而导致的。可按如下步骤进行修复:
确保已将Nhibernate和依赖注入相关的NuGet程序包正确安装。
在Startup.cs文件的ConfigureServices方法中,添加以下代码:
services.AddNHibernate(Configuration.GetConnectionString("DefaultConnection"));
.AddAutoMappings(typeof(Startup).Assembly) .AddAssembly(typeof(Startup).Assembly) .AddFluentNHibernateMappings() .ConfigureNHibernate(Configuration.LoadConfiguration().GetSection("NHibernate"));
services.AddScoped(factory =>
{
var httpContextAccessor = factory.GetService
return sessionFactory.WithOptions().Interceptor(new SessionContextInterceptor(httpContextAccessor)).OpenSession();
});
完成以上步骤后,应该能够成功注入和使用ISession实例。
上一篇:ASP.NETCoreMVC:在生成Identity时,EFCore迁移失败“BulidFailed”
下一篇:ASP.NETCoreNuGET软件包Microsoft.AspNetCore.Hosting版本2.2.7是ASP.NETCore5.0的一部分吗?