2021-01-02 09:16 阅读 1

NHibernate Environment.Version Error: The type initializer for 'NHibernate.Context.ReflectiveHttpContext' threw an exception.

Hi I´m facing a problem when trying to create a session factory with WebSessionContext. I had the same problem with the last two NHibernate versions (5.0.0 and 5.0.0-ci-00140-dbg-NH-3807). My environment is based on .net 4.7, but in .net core 2.0 is the same.

My problem is, when trying to get the the Session of the CurrentSessionContext, it returns the error below:

threw an exception of type 'System.TypeInitializationException' NHibernate.ISession

The inner Exception is:

Could not load file or assembly 'System.Web, Version=4.0.30319.42000, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

In my researches, I´ve found out that the problem can be related to the way the version of System.Web is returned in the following NHibernate method in NHibernate.Context.ReflectiveHttpContext:

private static System.Type HttpContextType
                    "System.Web.HttpContext, System.Web, Version={0}, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",

As you can check here, Microsoft doesn´t recommend to use this property (Environment.Version) anymore.

For the .NET Framework 4.5 and later, we do not recommend using the Version property to detect the version of the runtime; instead, you can determine the version of the common language runtime by querying the registry. For more information, see How to: Determine Which .NET Framework Versions Are Installed.

Is there a way to solve this?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • weixin_39828198 weixin_39828198 2021-01-02 09:16

    To provide some context, the way it is implemented is due to NH-2335 followed by NH-2339. Since currently NHibernate does no more support runtime 2.0 but only 4.0, this implementation does not make much sens anymore.

    Anyway, regarding your issue, can you confirm your application has an explicit reference to .Net Framework 4 System.Web assembly?

    点赞 评论 复制链接分享
  • weixin_39771775 weixin_39771775 2021-01-02 09:16

    Thanks Frederic. Actually, I have an explicit reference to .Net 4 System.Web. The problem is when I try to build a SessionFactory, and then when I try to check the SessionFactory to do a binding in the Session, I get the exception. The exception is in the image attached.

    To remember, I´m using .Net 4.7.

    Code to create the SessionFactory:

    var SessionFactory = configuration.BuildSessionFactory();
    var session = SessionFactory.OpenSession();

    Check to bind:

    if (!CurrentSessionContext.HasBind(SessionFactory))

    Please let me know if I´m not clear enough. I´m still trying to find the words to describe the problem porperly. nhibernate_exception

    点赞 评论 复制链接分享
  • weixin_39821605 weixin_39821605 2021-01-02 09:16

    Same here. I could not reproduce this. I tried 4.6.1 and 4.7 both under full .NET Framework and Mono.

    点赞 评论 复制链接分享