本文介绍了Web应用程序的Application_Start方法中初始化NServiceBus时的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行NServiceBus的2.0 RTM当我的MessageModule绑定CurrentSessionContext我的NHibernate的SessionFactory我得到一个NullReferenceException。

I am running the 2.0 RTM of NServiceBus and am getting a NullReferenceException when my MessageModule binds the CurrentSessionContext to my NHibernate sessionfactory.

从我的Application_Start内,我叫下面的方法:

From within my Application_Start, I call the following method:

public static void WithWeb(IUnityContainer container)
{
    log4net.Config.XmlConfigurator.Configure();

    var childContainer = container.CreateChildContainer();

    childContainer.RegisterInstance<ISessionFactory>(NHibernateSession.SessionFactory);

    var bus = NServiceBus.Configure.WithWeb()
        .UnityBuilder(childContainer)
        .Log4Net()
        .XmlSerializer()
        .MsmqTransport()
        .IsTransactional(true)
        .PurgeOnStartup(false)
        .UnicastBus()
        .ImpersonateSender(false)
        .LoadMessageHandlers()
        .CreateBus();

    var activeBus = bus.Start();

    container.RegisterInstance(typeof(IBus), activeBus);
}

当公交车启动时,我的留言模块具有以下启动:

When the bus is started, my message module starts with the following:

public void HandleBeginMessage()
{
    try
    {
        CurrentSessionContext.Bind(_sessionFactory.OpenSession());
    }
    catch (Exception e)
    {
        _log.Error("Error occurred in HandleBeginMessage of NHibernateMessageModule", e);
        throw;
    }
}

在看我的日志,我们会记录当绑定方法被调用了以下错误:

In looking at my log, we are logging the following error when the bind method is called:

System.NullReferenceException: Object reference not set to an instance of an object.
at NHibernate.Context.WebSessionContext.GetMap()
at NHibernate.Context.MapBasedSessionContext.set_Session(ISession value)
at NHibernate.Context.CurrentSessionContext.Bind(ISession session)

显然,有在获得HttpContext的一些问题。如果此调用配置NServiceBus发生在生命周期比的Application_Start以后呢?还是有其他人已经习惯了得到处理的Asp.NET Web应用程序中工作?另一个解决办法

Apparently, there is some issue in getting access to the HttpContext. Should this call to configure NServiceBus occur later in the lifecycle than Application_Start? Or is there another workaround that others have used to get handlers working within an Asp.NET Web application?

谢谢,
史蒂夫

Thanks,Steve

推荐答案

我不会在此情况下,$ P $使用WebSessionContext pcisely因为NServiceBus可以独立HttpContexts的操作。如果你要使用Web和NServiceBus消息处理单个会话的上下文实现,我会实施 NHibernate.Context.ICurrentSessionContext 与混合存储,也就是说,如果HttpContext.Current != NULL,可以使用的HttpContext作为会话存储。否则,使用一个线程本地存储。这是类似于城堡ActiveRecord的其 HybridWebThreadScopeInfo

I wouldn't use WebSessionContext in this case, precisely because NServiceBus can operate independently of HttpContexts. If you want to use a single session context implementation for both web and NServiceBus message handling, I'd implement NHibernate.Context.ICurrentSessionContext with an hybrid storage, i.e. if HttpContext.Current != null, use the HttpContext as session storage. Otherwise use a thread local storage. This is similar to what Castle ActiveRecord does with its HybridWebThreadScopeInfo.

这篇关于Web应用程序的Application_Start方法中初始化NServiceBus时的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:03