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.
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.