15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1378 ❘ ChaPTer 46 messAGe QueuinG<br />

must be called. To cancel a transaction (<strong>and</strong> have no messages written to the queue), the Abort()<br />

method must be called, as you can see within the catch block:<br />

using System;<br />

using System.Messaging;<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

if (!MessageQueue.Exists(@".\MyTransactionalQueue"))<br />

{<br />

MessageQueue.Create(@".\MyTransactionalQueue", true);<br />

}<br />

var queue = new MessageQueue(@".\MyTransactionalQueue");<br />

var transaction = new MessageQueueTransaction();<br />

}<br />

}<br />

}<br />

try<br />

{<br />

transaction.Begin();<br />

queue.Send("a", transaction);<br />

queue.Send("b", transaction);<br />

queue.Send("c", transaction);<br />

transaction.Commit();<br />

}<br />

catch<br />

{<br />

transaction.Abort();<br />

}<br />

Fessage queuing WiTh WCf<br />

Chapter 43, “Windows Communication Foundation,” covers the architecture <strong>and</strong> core features of WCF.<br />

With WCF, you can configure a Message Queuing binding that makes use of the Windows Message Queuing<br />

architecture. With this, WCF offers an abstraction layer to Message Queuing. Figure 46-14 explains the<br />

architecture using a simple picture. The client application invokes a method of a WCF proxy to send a message<br />

to the queue. The message is created by the proxy. For the client developer, there’s no need to know that a<br />

message is sent to the queue. The client developer just<br />

invokes a method of the proxy. The proxy abstracts<br />

dealing with the classes from the System.Messaging<br />

namespace <strong>and</strong> sends a message to the queue. The<br />

MSMQ listener channel on the service side reads<br />

messages from the queue, converts them to method<br />

calls, <strong>and</strong> invokes the method calls with the service.<br />

Next, the Course Ordering application gets<br />

converted to make use of Message Queuing from<br />

a WCF viewpoint. With this solution, the three<br />

projects done earlier are modified, <strong>and</strong> one more<br />

assembly is added that includes the contract of the<br />

WCF service:<br />

➤<br />

The component library ( CourseOrder)<br />

includes entity classes for the messages that<br />

Sender<br />

Proxy<br />

figure 46-14<br />

Service<br />

(Receiver)<br />

MSMQ<br />

Channel<br />

Listener<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!