在我和团队正在进行的一个新项目中,我正在探索NHibernate 5.2.7/FluentHibernate 2.1.2是否与ASP.NET Core 3.1兼容。我对NHibernate 5.2.3做了一些研究,这是一个稍早的版本,在那个SO线程中,他们注意到NHibernat在这一点上应该与.NET Core兼容。
话虽如此,但情况似乎并非如此。
以下是我们如何使用NHibernate。我们使用这个DataContext
对象创建会话(当然是在using
块中,因为ISessions
是IDisposable
!(,然后使用QueryOver获取或设置数据库中的数据。
然而,当我们尝试任何DB操作时,我们在_sessionFactory.OpenSession()
调用上会得到以下异常:
Could not load type 'System.Runtime.Remoting.Messaging.CallContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Inner exception: null
Stack trace: at NHibernate.Impl.SessionIdLoggingContext.get_SessionId()
at NHibernate.Impl.SessionIdLoggingContext..ctor(Guid id)
at NHibernate.Impl.AbstractSessionImpl.BeginContext()
at NHibernate.Impl.AbstractSessionImpl..ctor(ISessionFactoryImplementor factory, ISessionCreationOptions options)
at NHibernate.Impl.SessionImpl..ctor(SessionFactoryImpl factory, ISessionCreationOptions options)
at NHibernate.Impl.SessionFactoryImpl.SessionBuilderImpl`1.OpenSession()
at NHibernate.Impl.SessionFactoryImpl.OpenSession()
at MyRadProject.Core.DataContext.GetSession() in C:MyRadProject.CoreDataContext.cs:line 18
at MyRadProject.Core.Repositories.UserRepository.GetUsers(Boolean includeInactive) in C:MyRadProject.CoreRepositoriesUserRepository.cs:line 23
at MyRadProject.Web.Controllers.CommonController.GetUsers() in C:MyRadProject.WebControllersCommonController.cs:line 21
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<<InvokeActionMethodAsync>g__Logged|12_1>d.MoveNext()
据我的团队所知,由于.NET Core中没有提供某些名称空间或类(例如System.Configuration.ConfigurationManager
(,以前在.NET Core的早期版本中发生了此异常。由于.NET Core和NHibernate的共同功劳,这些问题似乎得到了很大缓解。
我仔细检查了我们的数据访问代码所在的库项目("MyAwesomeProject.Core"(中是否引用了mscorlib
。在Visual Studio 2019中,我没有看到这样的引用;当我尝试手动引用mscorlib
时,我得到一条消息,"mscorlib由构建过程自动引用。">
所有这些都引出了我的问题:
假设上面的SO线程准确地说明了NHibernate 5.1.1+与.NET Core兼容,那么我还需要做什么才能使NHibernat 5.2.7在尝试任何数据库操作时不会生成运行时异常
DataContext.cs
public class DataContext
{
private static ISessionFactory _sessionFactory;
private static bool _startupComplete;
private static readonly object _locker = new object();
public static ISession GetSession()
{
EnsureStartup();
ISession session = _sessionFactory.OpenSession(); // EXCEPTION occurs here!
session.BeginTransaction();
return session;
}
public static void EnsureStartup()
{
if (!_startupComplete)
{
lock (_locker)
{
if (!_startupComplete)
{
PerformStartup();
_startupComplete = true;
}
}
}
}
private static void PerformStartup()
{
InitializeSessionFactory();
}
private static void InitializeSessionFactory()
{
Configuration configuration = BuildConfiguration();
_sessionFactory = configuration.BuildSessionFactory();
}
public static Configuration BuildConfiguration()
{
var configuration = new Configuration().Configure();
return Fluently.Configure(configuration)
.Mappings(cfg => cfg.FluentMappings.AddFromAssembly(typeof(DataContext).Assembly))
.BuildConfiguration();
}
// There are other methods below this point that aren't pertinent to the question.
}
您似乎使用的是库的完整.NET Framework版本,因为CallContext
类未在.NET Core/.NET Standard版本的NHibernate中使用(而是使用AsyncLocal
(:https://github.com/nhibernate/nhibernate-core/blob/c0f50429722b4e061a0c6b40b720db17d33fc85e/src/NHibernate/Impl/SessionIdLoggingContext.cs#L60-L65
因此,要解决您的问题,请尝试使用该库的.NET Core或.NET Standard版本。