HowTo: Messaging with MSMQ, an introduction

imageA short time ago I found MSMQ. To say it easy: MSMQ is a system where messages are able to be classified into queues and be converted piece by piece.

Why should I use this?

MSMQ is a system of queues. You put your message into it and than somebody will take it and convert it. This could be very useful in distributed applications for example. So for example it is possible to generate E-Mails, put them in a queue and send them out piece by piece so the server won´t be overloaded. Of course there are a lot more possibilities but this is just an introduction.

Later I´m going to talk about some more advantages and disadvantages and I would be very glad to get some comments from you.

Requirements

The infrastructure is included every ware since XP (?). You only need to activate it in the windows functions:

image

Afterwards you are able to see MSMQ Services in the Computer-Management and Service:

image

Click right and you will be able to look into already existing queues and to create new ones:

image

Here it is also possible to take a look into the several massages:

image

For my demo code it is necessary to create the “test” queue beneath the private queues.

The Code:

In .NET Framework you will find everything you need to start:

image

Write a new message into the queue:

            MessageQueue queue = new MessageQueue(@".\private$\test");
            queue.Send("test");

Read the message:

MessageQueue queue = new MessageQueue(@".\private$\test");
Console.WriteLine(queue.Receive().Body.ToString());

Easy, isn´t it? If you click on “receive” the first message of the queue will be chosen.

Afterwards the message won´t be reachable for other clients as well.

Extensive file types:

Of course it is possible to send extensive file types as well. These file types will be written into the message by the XmlSerializer.

Here is my Example:

using System;
using System.Collections.Generic;
using System.Messaging;
using System.Text;

namespace MSMQ
{
    public class User
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Simple Text");
            Console.WriteLine("Input for MSMQ Message:");
            string input = Console.ReadLine();

            MessageQueue queue = new MessageQueue(@".\private$\test");

            queue.Send(input);

            Console.WriteLine("Press Enter to continue");
            Console.ReadLine();

            Console.WriteLine("Output for MSMQ Queue");
            Console.WriteLine(queue.Receive().Body.ToString());
            Console.ReadLine();

            Console.WriteLine("Complex Text");

            User tester = new User();
            tester.Birthday = DateTime.Now;
            tester.Name = "Test Name";
            queue.Send(tester);

            Console.WriteLine("Output for MSMQ Queue");
            User output = (User)queue.Receive().Body;
            Console.WriteLine(output.Birthday.ToShortDateString());
            Console.ReadLine();

        }
    }
}

It works. And now? WHY should I use MSMQ?

You are right. It is possible to make this whole queue thing with SQL ore other self made applications. So why should you take a look on MSMQ?

Advantages- and Disadvantages: (took it from the stack overflow)

Cons:

  • Each queue can only be 2GB.
  • Each message 4MB (altough the 4MB limit can be fixed by using MSMQ with WCF).
  • Only for Windows so you´re limited to use it with .NET, C/C++ or COM library for COM-enabled environments.

Pros:

  • Supports Windows Network Load Balancer.
  • Supports Microsoft Cluster Service.
  • Integrated with Active Directory.
  • Ships with Windows.
  • Supports transactions.
  • MSMQ messages can be tracked by audit messages in the Windows Event log.
  • Messages can be automatically authenticated (signed) or encrypted upon sending, and verified and decrypted upon reception.

Questions to you…

Do you use MSMQ already? Ore do you prefer a simple SQL data base? Of course the pros sound nice but isn´t this possible with SQL as well? Are you having any more extensive examples?

In my opinion it isn´t easy to clarify which option is the better one but maybe some of you know about some additional frameworks ore some absolutely no-go´s? ;)

Read more

HowTo: Logging with Log4Net

As soon as the application works your customer will find the first bugs. In this case it is very important to find out what happened and to find the mistake. Because of this it is helpfully to include a logging while you are debugging. Log4Net is a very classy library which fulfils you every desire […]

Read more

HowTo: Time-controlled & recurrent functions in .NET & ASP.NET

During a project we had the order to run a specific SQL request after several minutes or seconds and to evaluate them in order to the results. You can solve this problem a little bit “dirty” by using a while(true) loop and Thread.Sleep, or you use a timer. Example: For example we are going to […]

Read more

HowTo: Facebook Connect & ASP.NET MVC

Facebook has created a nice little service named "Facebook Connect". With this application Facebook user are allowed to login into your site with their usual facebook log-in information´s and the whole thing is secured by Facebook. In Fact it´s quite similar to OpenID but it had some more specials than just identification. But in this […]

Read more

HowTo: Cross Domain Ajax with JSONP and ASP.NET

Actually it isn´t possible to send Ajax Requests to addresses which are reachable at the same domain like the site where the script is executed.The reason for this is the Same Origin Policy in JavaScript. There it is given, that the port, the protocol and the domain have to be equal to start requests. This […]

Read more

HowTo: Introduction in to MetaWebLog API

This Blogspot is based on Scott Hanselmans “WeeklySourceCode”, where MetaWebLog API was the subject of today. In the Blogspot he relates that MS use the Windows Live Writer also to administrate their own websites. The tool is absolutely practical and it works with MetaWebLog API. With this tool it´s possible to create “CMS” Content with […]

Read more

Windows 7 RC Training Kit for Developer

Microsoft released the Training Kit for developer yesterday. It contains presentations, Hands on Labs and democode for the new Windows 7 APIs. The Traing Kit is directed towards to C++ developers, but Microsoft offers Wrapper classen for .NET. Topics: Taskbar Libraries Multi Touch Sensors and Location Ribbon Trigger Start Services, Instrumentation and ETW Application Compatability […]

Read more

Visual Studio 2010 & .NET Framework 4 Training Kit – May Preview

Microsoft released a preview Training Kit for .NET 4.0 & VS 2010: Download. It includes 28 presentations, 13 demos & 16 Hands-on Labs.   Overview The Visual Studio 2010 and .NET Framework 4 Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the Visual Studio […]

Read more

Inside Microsoft’s home of the future

Microsoft shows in a Video how the “home of the future” might look like. Some photos of this house could be found here. Speak recognition & “NUI“s are “key features” in this house.  

Read more

Free eBook: Practical .NET Unit Testing

Nice intro to unit testing (& mocking) with .NET: – Practical .NET Unit Testing

Read more

Recent Posts

  • HowTo: make out Retweet-counter in a random URL

      Twitter offers some interesting functions with the integration of the twitter button – every user can see how often the website has been retweeted and he can do the same with just one click. But of course the whole thing has his price: - The integration of the Twitter JavaScript makes the whole data […]

  • Time-controlled Azure WebJobs – Azure as easy as it get‘s

        While still in development the Azure WebJob SDK offers some cool features for procession and supply of information. A good example is the sample that observes the Azure Queue and processes an item as soon as it spots one.   Scenario: time-controlled activities – without queue and so on My scenario was quite […]

  • image2021-570x194_thumb.png
    HowTo: Create RSS Feeds with ASP.NET MVC

    I’ve already written about how to consume RSS or Atom Feeds with the SyndicationFeed (in German, sry) but todays post is about how to create or publish an own feed. Beside the age of the feed standard and the much defamation from Facebook, Twitter and Google on RSS/Atom I consider this easy to consume API […]

  • image1997-311x194_thumb.png
    How to access an Azure Website with the local IIS Manager

    Since the end of February it is possible to access an Azure Website with the IIS Manager. Although the Azure Management site offers some information there are more details visible at the IIS Manager. For the connection you will need an IIS Manager and the IIS Manager for Remote Administration Extension. It’s also possible to […]

  • image1929-570x143_thumb.png
    Create and validate own Json-Web-Tokens (JWTs)

    If you are interested in web authentication you probably have heard about JSON Web tokens (JWT). What is a JWT? Maybe I’m not using the correct security termination but however: JWTs are used to exchange claims between two systems. For example: You want to log on to a service (like Facebook, Twitter, etc.) and want […]

Support us