Tuesday, October 6, 2015

Every time I try to work with NServiceBus I get a little bit closer to success.

The MeasureUP talk I liked the best was by a Mr. Justin Self and it was a very entry level how-to on NServiceBus. I came out of the talk with a basic understanding of how to set up a project that used NServiceBus and I then wrote some silly (you'll see how silly) code of my own. I'm pretty sure that the code I have would work if I could figure out how to get NServiceBus to see MSMQ. I "put my hand up" (well, I opened my big mouth) during the talk and asked if there was something akin to the connection string in a Web.config that finds the database for an elementary web site that specified where to look to for MSMQ and how to connect to it. Justin said that there is something like that if you use RabbitMQ, but if you are just using MSMQ at the same server that you are running the app with NServiceBus it will just be found. I turned it on and I can't see it however. Anyways, my message object looks like this:

using System;
using NServiceBus;
namespace SomethingFun.Models
{
   public class Chicken : ICommand
   {
      public DateTime Hatching { get; set; }
   }
}

 
 

"Install-Package NServiceBus" as a NuGet command loops in the NServiceBus namespace here. That and just turning on MSMQ (and yes you have to explicitly turn it on as a service) should be all of the prep work you have to do. In Windows Server 2012 R2 , if you go to the Server Manager, and pick "Computer Management" from the "Tools" menu at the upper right, you should be able to see "Messaging Queuing" beneath "Services and Applications." I think "Add Roles and Features" beneath "Manage" back at Server Manager is where you add in MSMQ to begin with. My idea was to make an ASP.NET MVC application that, upon spin up, asynchronously reached out to ipchicken.com and scraped the page to sniff out the IP address which it would then write off to a text file. I didn't really get that far however because I, again, hit the brick wall of not being able to speak to MSMQ. You are going to need an implementation of IHandleMessages and mine looked like so:

using System;
using NServiceBus;
using SomethingFun.Models;
namespace SomethingFun
{
   public class ChickenScraper : IHandleMessages<Chicken>
   {
      public void Handle(Chicken message)
      {
         throw new Exception("whatever");
      }
   }
}

 
 

When I actually run my application the error I get specifically suggests that I cannot find a queue at MSMQ for my implementation of IHandleMessages. Rats! Anyways my HomeController just looks like this:

using System;
using System.Web.Mvc;
using NServiceBus;
using SomethingFun.Models;
namespace SomethingFun.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         var bus = Bus.CreateSendOnly(new BusConfiguration());
         Chicken chicken = new Chicken();
         chicken.Hatching = DateTime.Now;
         bus.Send(chicken);
         return View();
      }
   }
}

 
 

Your configuration file needs some love to get this all working. Well, heh, I don't have it working, but at least I have it a little ways along. To get to the point where you cannot talk to MSMQ you will need something like the following in the configSections portion of Web.config or app.config or whatever:

<section name="MessageForwardingInCaseOfFaultConfig"
      type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig,
      NServiceBus.Core"/>
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig,
      NServiceBus.Core"/>
<section name="AuditConfig" type="NServiceBus.Config.AuditConfig,
      NServiceBus.Core"/>

 
 

Finally, the end of the configuration file should look like...

   <MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
   <UnicastBusConfig>
      <MessageEndpointMappings>
         <add Assembly="SomethingFun" Type="SomethingFun.Models.Chicken"
               Endpoint="SomethingFun.ChickenScraper" />
      </MessageEndpointMappings>
   </UnicastBusConfig>
   <AuditConfig QueueName="audit" />
</configuration>

No comments:

Post a Comment