15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Message Queuing with WCf ❘ 1383<br />

<br />

<br />

<br />

<br />

<br />

The Click event h<strong>and</strong>ler of the buttonProcessOrder button removes the selected course order from the<br />

collection class:<br />

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

{<br />

CourseOrder courseOrder = listOrders.SelectedItem as CourseOrder;<br />

courseOrders.Remove(courseOrder);<br />

listOrders.SelectedIndex = -1;<br />

buttonProcessOrder.IsEnabled = false;<br />

}<br />

MessageBox.Show("Course order processed", "Course Order Receiver",<br />

MessageBoxButton.OK, MessageBoxImage.Information);<br />

code snippet CourseOrderReceiver/CourseOrderReceiverWindow.xaml.cs<br />

WCf message sender application<br />

The sending application is modified to make use of a WCF proxy class. For the contract of the service,<br />

the assembly CourseOrderServiceContract is referenced, <strong>and</strong> the assembly System.ServiceModel is<br />

required for use of the WCF classes.<br />

In the Click event h<strong>and</strong>ler of the buttonSubmit control, the ChannelFactory class returns a proxy.<br />

The proxy sends a message to the queue by invoking the method AddCourseOrder():<br />

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

{<br />

try<br />

{<br />

var order = new CourseOrder<br />

{<br />

Course = new Course()<br />

{<br />

Title = comboCourses.Text<br />

},<br />

Customer = new Customer()<br />

{<br />

Company = textCompany.Text,<br />

Contact = textContact.Text<br />

}<br />

};<br />

var factory = new ChannelFactory("queueEndpoint");<br />

ICourseOrderService proxy = factory.CreateChannel();<br />

proxy.AddCourseOrder(order);<br />

factory.Close();<br />

MessageBox.Show("Course order submitted", "Course Order",<br />

MessageBoxButton.OK, MessageBoxImage.Information);<br />

}<br />

catch (Exception ex)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!