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.

Course order application ❘ 1371<br />

The file Customer.cs includes the class Customer, which includes properties for the company <strong>and</strong><br />

contact names:<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

public class Customer<br />

{<br />

public string Company { get; set; }<br />

public string Contact { get; set; }<br />

}<br />

}<br />

code snippet CourseOrder/Customer.cs<br />

The class CourseOrder in the file CourseOrder.cs maps a customer <strong>and</strong> a course inside an order <strong>and</strong><br />

defines whether the order is high priority. This class also defines the name of the queue that is set to a<br />

format name of a public queue. The format name is used to send the message, even if the queue cannot be<br />

reached currently. You can get the format name by using the Computer Management snap-in to read the ID<br />

of the message queue. If you don’t have access to an Active Directory to create a public queue, you can easily<br />

change the code to use a private queue.<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

public class CourseOrder<br />

{<br />

public const string CourseOrderQueueName =<br />

"FormatName:Public=D99CE5F3-4282-4a97-93EE-E9558B15EB13";<br />

}<br />

}<br />

public Customer Customer { get; set; }<br />

public Course Course { get; set; }<br />

code snippet CourseOrder/CourseOrder.cs<br />

Course order message sender<br />

The second part of the solution is a Windows application called<br />

CourseOrderSender. With this application, course orders are sent<br />

to the message queue. The assemblies System.Messaging <strong>and</strong><br />

CourseOrder must be referenced.<br />

The user interface of this application is shown in Figure 46-10. The<br />

items of the combo box comboBoxCourses include several courses<br />

such as Advanced .<strong>NET</strong> Programming, Programming with LINQ,<br />

<strong>and</strong> Distributed Application Development using WCF.<br />

When the Submit the Order button is clicked, the h<strong>and</strong>ler<br />

method buttonSubmit_Click() is invoked. With this method, a<br />

CourseOrder object is created <strong>and</strong> filled with the content from the<br />

TextBox <strong>and</strong> ComboBox controls. Then a MessageQueue instance is<br />

created to open a public queue with a format name. With the Send()<br />

method, the CourseOrder object is passed to serialize it with the<br />

default XmlMessageFormatter <strong>and</strong> to write it to the queue:<br />

figure 46-10<br />

private void buttonSubmit_Click(object sender, RoutedEventArgs e)<br />

{<br />

try<br />

{<br />

var order = new CourseOrder();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!