订阅者无法处理来自发布者的消息

发布于 2024-12-27 17:53:31 字数 3734 浏览 2 评论 0原文

我正在 ASP.NET MVC3 上测试 NServiceBus。目前,我可以向后端发送命令消息,并且消息处理程序正在侦听它。但是当我尝试发布相同的消息时,消息处理程序没有监听。

我发现的问题是消息没有发布到订阅者的队列中。当我调试并检查 NServiceBusHost 控制台时,它说

2012-01-19 22:53:35,042 [1] INFO  NServiceBus.Unicast.UnicastBus [(null)] <(null
)> - Subscribing to SampleMessage.Person1WasCreated, SampleMessage, Version=1.0.
0.0, Culture=neutral, PublicKeyToken=null at publisher queue Test-web

我几天来一遍又一遍地检查我的配置设置,这对我来说似乎是正确的。谁能帮我检查一下我缺少什么吗?我想做的事情是从网络发布消息,并且该消息应该在后端(ServiceHost2)处理。

ASP.NET MVC3 Web.config

<configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="MsmqSubscriptionStorageConfig" type="NServiceBus.Config.MsmqSubscriptionStorageConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core"/>
  </configSections>
  <MsmqTransportConfig InputQueue="Test-web" ErrorQueue="Test-web-error" NumberOfWorkerThreads="1" MaxRetries="5" />
  <MsmqSubscriptionStorageConfig Queue="Test-subscriptionstorage" />

Global.asax.cs

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            Global.Bus = Configure.WithWeb()
               .Log4Net()
                .CastleWindsorBuilder()
                .XmlSerializer()
                .MsmqTransport().IsTransactional(false)
                .UnicastBus()
                .LoadMessageHandlers()
                .MsmqSubscriptionStorage()
                .CreateBus()
                .Start()
                ;
        }

ServiceHost2 项目中的 App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="MsmqSubscriptionStorageConfig" type="NServiceBus.Config.MsmqSubscriptionStorageConfig, NServiceBus.Core" />
  </configSections>
  <MsmqSubscriptionStorageConfig Queue="Service2-AC1-subscriptionstorage" />
  <MsmqTransportConfig InputQueue="Service2-AC1" ErrorQueue="Service2-AC1-Error" NumberOfWorkerThreads="1" MaxRetries="5" />
  <UnicastBusConfig>
    <MessageEndpointMappings>
    <add Messages="SampleMessage" Endpoint="Test-web" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
  <appSettings>
    <add key="EndpointConfigurationType" value="ServiceHost2.EndpointConfig, ServiceHost2"/>
  </appSettings>
</configuration>

的 EndpointConfig

namespace ServiceHost2
{
    public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
    {
        public void Init()
        {
            Configure.With()
                .Log4Net()
                .CastleWindsorBuilder()
                .MsmqTransport()
                .MsmqSubscriptionStorage()
                .XmlSerializer()
                .UnicastBus().LoadMessageHandlers()
                ;
        }
    }
}

ServiceHost2控制器中发布消息

public ActionResult CreatePerson(Person p)
        {
            var Person1WasCreated = new Person1WasCreated {Id = p.Id,Name = p.Name};
            Global.Bus.Publish(Person1WasCreated);
            return View();
        }

方法谢谢。

I'm testing NServiceBus on ASP.NET MVC3. At the moment, I'm able to send command message to the backend and the message handler is listening to it. But when i try to publish the same message, the message handler is not listening.

The problem i found is that message is not published to the subscriber's queue. When i debug and check NServiceBusHost console, it says

2012-01-19 22:53:35,042 [1] INFO  NServiceBus.Unicast.UnicastBus [(null)] <(null
)> - Subscribing to SampleMessage.Person1WasCreated, SampleMessage, Version=1.0.
0.0, Culture=neutral, PublicKeyToken=null at publisher queue Test-web

I checked my config settings again and again for few days and it seems correct for me . Can anyone please check for me what I'm missing? The thing I'm trying to do is to publish a message from the web and the message should be handle at the backend (ServiceHost2).

ASP.NET MVC3 Web.config

<configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="MsmqSubscriptionStorageConfig" type="NServiceBus.Config.MsmqSubscriptionStorageConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core"/>
  </configSections>
  <MsmqTransportConfig InputQueue="Test-web" ErrorQueue="Test-web-error" NumberOfWorkerThreads="1" MaxRetries="5" />
  <MsmqSubscriptionStorageConfig Queue="Test-subscriptionstorage" />

Global.asax.cs

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            Global.Bus = Configure.WithWeb()
               .Log4Net()
                .CastleWindsorBuilder()
                .XmlSerializer()
                .MsmqTransport().IsTransactional(false)
                .UnicastBus()
                .LoadMessageHandlers()
                .MsmqSubscriptionStorage()
                .CreateBus()
                .Start()
                ;
        }

App.Config in ServiceHost2 Project

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" />
    <section name="MsmqSubscriptionStorageConfig" type="NServiceBus.Config.MsmqSubscriptionStorageConfig, NServiceBus.Core" />
  </configSections>
  <MsmqSubscriptionStorageConfig Queue="Service2-AC1-subscriptionstorage" />
  <MsmqTransportConfig InputQueue="Service2-AC1" ErrorQueue="Service2-AC1-Error" NumberOfWorkerThreads="1" MaxRetries="5" />
  <UnicastBusConfig>
    <MessageEndpointMappings>
    <add Messages="SampleMessage" Endpoint="Test-web" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
  <appSettings>
    <add key="EndpointConfigurationType" value="ServiceHost2.EndpointConfig, ServiceHost2"/>
  </appSettings>
</configuration>

EndpointConfig for ServiceHost2

namespace ServiceHost2
{
    public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
    {
        public void Init()
        {
            Configure.With()
                .Log4Net()
                .CastleWindsorBuilder()
                .MsmqTransport()
                .MsmqSubscriptionStorage()
                .XmlSerializer()
                .UnicastBus().LoadMessageHandlers()
                ;
        }
    }
}

Method in the controller which publish the message

public ActionResult CreatePerson(Person p)
        {
            var Person1WasCreated = new Person1WasCreated {Id = p.Id,Name = p.Name};
            Global.Bus.Publish(Person1WasCreated);
            return View();
        }

Thanks.

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

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

发布评论

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

评论(1

雨的味道风的声音 2025-01-03 17:53:31

我想我找到了原因。我今天读了这篇关于 为什么不从 Web 应用程序发布 NServiceBus 消息?

它非常清楚地表明,消息不应该从 Web 发布。不知何故,我的网络项目创建的订阅存储不允许任何内容订阅它。

所以我的解决方案是,我将命令消息从 Web 发送到 Service1 中的消息处理程序,并向 Service2 发布新的事件消息。甚至事件消息也能够订阅到 Service1 的订阅存储中。现在一切正常。谢谢。

I think I found out why. I was reading on this article today about Why not publish NServiceBus messages from a web application?

It states very clear that, message is not suppose to publish from the web. Somehow The subscription storage created by my web project doesn't allow anything to subscribe to it.

So my solution is, I send the command message from the web to a message handler in Service1, and publish a new event message to Service2. Even the event message is able to subscribe into Service1's subscription storage.Everything works fine now. Thanks.

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