在 Web 应用程序 Application_Start 方法中初始化 NServiceBus 时出现 NullReferenceException

发布于 2024-08-30 19:47:50 字数 1688 浏览 4 评论 0原文

我正在运行 NServiceBus 2.0 RTM,当我的 MessageModule 将 CurrentSessionContext 绑定到我的 NHibernate sessionfactory 时,我收到 NullReferenceException。

在我的 Application_Start 中,我调用以下方法:

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);
}

当总线启动时,我的消息模块以以下内容开头:

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

在查看我的日志时,我们在调用绑定方法时记录以下错误:

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 应用程序中工作?

谢谢, 史蒂夫

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

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-09-06 19:47:50

在这种情况下我不会使用 WebSessionContext,正是因为 NServiceBus 可以独立于 HttpContexts 进行操作。如果您想使用单个会话上下文实现来处理 Web 和 NServiceBus 消息,我将使用混合存储实现 NHibernate.Context.ICurrentSessionContext ,即如果 HttpContext.Current != null,则使用HttpContext 作为会话存储。否则使用线程本地存储。这类似于 Castle 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文