DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Boosting Application Performance With MicroStream and Redis Integration
  • Comparing Cloud Hosting vs. Self Hosting
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • Introduction To Git

Trending

  • Boosting Application Performance With MicroStream and Redis Integration
  • Comparing Cloud Hosting vs. Self Hosting
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • Introduction To Git

Quick Start on Mass Transit and MSMQ on Windows

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Aug. 16, 12 · Interview
Like (0)
Save
Tweet
Share
11.22K Views

Join the DZone community and get the full member experience.

Join For Free

MassTransit is a bus implementation for .NET that promises frictionless configuration and simple usage. The main problem about MassTransit in my opinion is the lack of organic documentation, especially a quick start guide that shows you the basic concepts behind MassTransit. If you jump into online documentation there is a nice page called “Show Me the code” that promise a quick start to jump into MassTransit concepts. The problem with that example is that it shows a single snippet of code that opens a bus based on Windows MSMQ and send a message to itself.

In my opinion, this is a too simplistic “Hello MassTransit” example, because it does not show a typical sender/receiver configuration. If you try to write another simple console application to send message to the first one you build following the “Show me the code” instruction you will need this code.

Bus.Initialize(sbc =>
{
    sbc.UseMsmq();
    sbc.VerifyMsmqConfiguration();
    sbc.UseMulticastSubscriptionClient();
    sbc.ReceiveFrom("msmq://localhost/test_queue_client");
    sbc.ConfigureService<RoutingConfigurator>(
        BusServiceLayer.Session,
        rc => rc.Route<YourMessage>().To("msmq://localhost/simple_first_server")
        );
});
Bus.Instance.Probe();
Bus.Instance.WriteIntrospectionToConsole();
String read;
while (!String.IsNullOrEmpty(read = Console.ReadLine()))
{
    Bus.Instance.Publish(new YourMessage { Text = read });
}

The code is really similar to the code of the other application, it changes the ReceiveFrom address and adds the sbs.ConfigureService to rout the YourMessage to the queue used by the server. Something worth noticing in this example: the ConfigureService method is obsolete and deprecated and the reason is that this is not the typical scenario MassTransit want to solve. The problem with this code is that it is modeling a classic SOA model, where a client sends messages to a well known service and such a situation is so simple that you probably do not need a bus to handle it. The real feature that MassTransit offers you is the ability to just send and manage Messages from your applications, leaving all the gory details to the bus infrastructure.

To create a more interesting example add this line to the configuration of the first application, that one that subscribed to the YourMessage message.

sbc.UseSubscriptionService("msmq://localhost/mt_subscriptions");
sbc.UseSubscriptionService("msmq://localhost/mt_subscriptions");

These lines tell MassTransit to handle subscription of messages through the queue called mt_subscriptions. Now modify the other application console substituting the call to sbc.ConfigureService with the same above line. This will remove the need to specify the address of the receiver. Now the application can simply publish messages to the bus with the Bus.Instance.Publish call and let MassTransit take care of everything, especially routing messages to the application that subscribed to that specific message.

Now the main question is: what is the mechanism that route the message from the sender application to the listening application? The answer is: MassTransit and this is one of the cool reason to use a bus instead of directly using MSMQ or using WCF. When the first application subscribe to the YourMessage message, this subscription is sent to the subscription service in the queue mt_subscriptions and when the other application publish the message, the subscription service is used to understand where the message should be routed. The routing service will check the incoming message that is a message of type YourMessage, then verify if there is anyone that is registered to listen to that message, if a match is found it routes the message to the right application. The problem is that if you run the above two programs you will get a nasty exception.

Failed to create the bus service: SubscriptionRouterService

Timeout waiting for subscription service to respond

The reason is that you need to run a third program, called MassTransit.RuntimeServices.exe that actually manages all the routing stuff. If you got MassTransit reference through NuGet you surely miss the runtimeservice components. The simpliest thing is downloading MassTransit source from GitHub, follow the instruction in the readme to build with build.bat and finally you will end with all you need in the folder MassTransitbuild_outputServicesRuntimeServices

The MassTransit.RuntimeServices.exe application needs a database to store data and since it uses nhibernate you can use almost any database you want, just edit the MassTransit.RuntimeService.exe.config file and configure nhibernate to access your preferred database. In my example I used SqlExpress, just create a database called MassTransitRuntimeServices and create the schema running the script file located in srcMassTransit.RuntimeServicesSetupSQLServer.sql. Once the database and the configuration file MassTransit.RuntimeService.Exe.config are modified correctly, you can run the service that basically is a simple console application that do the routing. Remember that you need to run it as administrator because it need some administrative permission to setup stuff (like creating queue).

When the subscription service is running well you can run both your applications and everything should work.

application

Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Boosting Application Performance With MicroStream and Redis Integration
  • Comparing Cloud Hosting vs. Self Hosting
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • Introduction To Git

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: