in

WPF Design and Development

IdentityMine Team Blogs

David Kelley

A day in the life of a humble software architect... doing C#, WPF, Silverlight, Legos, Fuzzy Logic AI and/or whatever is the latest and greatest or more importantly the coolest techo mumbo jumbo...

Inspiring WCF - Windows Communication Foundation and MSMQ

I know that allot of developers are getting excited about cool graphics and WPF and all and that is great. I mean check this link out: http://3dscreen.ramboll.dk/ I mean really this is cool stuff. But it seems to me that allot of developers are missing out on the allure of 'backend' technologies and how inspiring they are. For me a good 'Center of Gravity' statistical calculation on F.L. rule results sets to produce and A.I. decision is much much more inspiring. :) But ok back reality for a second. With the new round of technologies WCF and MSMQ are one of the great things coming out and I know it is inspiring for me. Several years ago with all the COM+ and related technologies things like transaction queuing and windows services and managing 'ports' etc was a big deal and not to the sponsors but to the developer. That was hard stuff especially when you got into C++ COM or even just ATL COM it got hard core. Windows Communication Foundation really gives us a unifying set of tools to build upon that allows us to do things quickly and effectively that used to be hugely complicated tasks.

Lets take the WCF MSMQ approach. All the wizbang functionality of the old MSMQ but much easier. The first step is to check if a queue exists and if it doesn't exist create the queue on the machine that will be using the queue. Now this of course means that you will need to install MSMQ on the local box but that's ok. :) So anyway our code should look something like this:

if (!MessageQueue.Exists("myqueue"))
{
     MessageQueue.Create("myqueue", true);
}

ah, but don't forget about the libraries we need. For MSMQ you should use the following:
 
using System.Messaging;
using System.ServiceModel.MsmqIntegration;

so the next step is to create a message (can be just a serialization class), a contract, and a message client, IService class which can just be a serializable class for example:

[Serializable]
public class MSMQMessage
{
     public string Id;
     public int Type;
     public DataSet Records;
}
 
[ServiceContract]
public interface IMyService
{
[OperationContract]
bool DispatchMessage(MSMQMessage Msg);
}
 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface IPortiaContextServiceChannel : IMyService, System.ServiceModel.IClientChannel { }
 
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class MyManagerClient : System.ServiceModel.ClientBase, IMyService
{
public Guid ID = Guid.NewGuid();
 
public MyManagerClient() { }
 
public MyManagerClient(string endpointConfigurationName) : base(endpointConfigurationName) { }
 
public MyManagerClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
 
public MyManagerClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
 
public MyManagerClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { }
 
public bool DispatchMessage( MSMQMessage Msg)
{
// We don't want to timeout
((IContextChannel)this.InnerChannel).OperationTimeout = new TimeSpan(24, 0, 0);
return base.Channel.DispatchMessage(Msg);
}
}
 
}

So basically that gives us our message and most of the plumbing to start passing stuff around. Now in our host process we just need to create a queue reader and wire it up to our queue. Usually we would wire it up in the same code that we create the queue in but we don't have to. In my next little sample we will spin up the queue and reader and then we are ready to take messages.

MessageQueue MyQueue = new MessageQueue("myqueue");
MyQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyQueueReader);
MyQueue.BeginReceive();

Now we should define the handler that we just associated with our queue.

public static void MyQueueReader(Object source, ReceiveCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue Queue = (MessageQueue)source;
// End the asynchronous receive operation.
System.Messaging.Message msg = Queue.EndReceive(asyncResult.AsyncResult);
msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(MSMQMessage) });
MSMQMessage MyMsg = (MSMQMessage)msg.Body;
string Id = MyMsg.Id;
//some code to do something...
Queue.BeginReceive();
}

ok so my reader here doesn't do much but you should get the idea. One key thing to remember if this blows up then your queue is going to back up. This means that you better be sure your code will not blow up and if not you should implement some kind of error handling. So now we can start passing messages and doing something with them. well except for one detail. We need to add configuration settings for our message queue endpoint and bindings. In the application that our readers lives in we need to add the following to the app.config:

in the system.serviceModel - client we need add the following:
 
<endpoint name="MyResponseEndpoint"
address="msmq.formatname:DIRECT=OS:.\private$\ScopeCompleteMessage"
binding="msmqIntegrationBinding"
bindingConfiguration="ScopeCompleteMessageBinding"
contract="Portia.Library.IScopeCompleteMessage" >
</endpoint>
 
then in the bindings - msmqIntegrationBinding section:
 
<binding name="InstanceMessageBinding" >
<security mode="None" />
</binding>

Now it would actually work. Now lets talk about how to send messages to the queue?

MSMQMessage myMSMQMessage = new MSMQMessage();
myMSMQMessage.Id = "23";
myMSMQMessage.Type = 2;
 
// send MSMQ message to spin instances up //
MsmqMessage MyMsmqMessage = new MsmqMessage(myMSMQMessage);
MessageClient MyManagerClient = new MessageClient("MyResponseEndpoint");
MyManagerClient.SubmitMessage(MyMsmqMessage);
MyManagerClient.Close();

So now we can create and manage the queue, read messages and do something and most importantly we can talk to the queue. MSMQ/WCF is a simple straight forward messaging technology/infrastructure that allows you to quickly and simply build and deploy MSMQ based solutions.

Published Nov 13 2006, 11:33 AM by david.kelley
Filed under:

Comments

No Comments

About david.kelley

David for the past 10 years has focused on distributed application design and emerging Microsoft technologies on the web. Having helped design and build some of the largest systems for companies like Microsoft, Onyx Software, Saltmine, Giordanous Group and more and of course our favorite company Identitymine, he has been on the leading edge of applying the latest tech to real world business problems. David’s technology breadth includes everything from SQL Server to Windows/WCF and Silverlight. David’s accomplishments also include developing new technologies such as self editing XML files and related XML technologies to fuzzy logic systems and advanced web user interface design.
© 2007 IdentityMine, Inc.
Powered by Community Server (Commercial Edition), by Telligent Systems