MSDN Magazine Launch Issue - February 15, 2008 - (Page 39) So far, we’ve designed the skeletal structure for our class, as shown in Figure 4. With that skeletal code written, you can proceed with writing the rest of the device and desktop applications. The full implementation of Messaging.cs is included in the source code download for this article. Nearly all of it is ordinary WCF messaging layer code without any regard for a mail transport. The next step is construction and teardown of the mail channel. Open the code behind the main form (Form1.cs). You’ll need to initialize the mail channel in the constructor and tear it down when the form is closed. To start, add a field that will track the Messaging instance. Since the app communicates using text messages, T will be a string: Messaging messaging; Figure 4 Simple Messaging Class using Microsoft.ServiceModel.Channels.Mail; class Messaging { public delegate void IncomingMessageCallback(T body); public const string DeviceSendChannel = “toDesktop”; public const string DesktopSendChannel = “toDevice”; public Messaging(MailBindingBase binding, string sendChannelName, string listenChannelName, IncomingMessageCallback incomingMessageCallback) { // } public void SendMessage(string recipient, T body) { // } public void Close() { // } You will initialize this field shortly. Since the Messaging constructor requires a callback, first you define a compatible method: void incomingMessage(string message) { } } Now initialize the messaging field in the constructor. Be careful to order the two channel names so that the device app sends messages using the DeviceToDesktopChannel name and listens on the DesktopToDeviceChannel name. That way, the device will never receive messages it sends to the desktop, even if the desktop and device are sharing a single e-mail address: messaging = new Messaging ( new WindowsMobileMailBinding(), Messaging .DeviceToDesktopChannel, Messaging .DesktopToDeviceChannel, (Messaging .IncomingMessageCallback)incomingMessage); Figure 5 Responding to Incoming Messages void incomingMessage(string message) { // Invoke ourselves on the UI thread if (InvokeRequired) { Invoke((Messaging .IncomingMessageCallback) incomingMessage, message); } else { // append incoming message to message history historyTextBox.Text += message + Environment.NewLine; // scroll to end of history window historyTextBox.Select(historyTextBox.Text.Length, 0); historyTextBox.ScrollToCaret(); } } Close the channel when the application exits in the Form.Closed event handler. The Form1_Closed method only has to call the Close method on the Messaging object: void Form1_Closed(object sender, EventArgs e) { messaging.Close(); } Creating and Sending the Message The device app is very nearly complete. All you need to do is send and respond to messages. You’ll need to add a handler for the Send button. The event handler should call SendMessage on the Messaging object and then clear the message textbox so the user knows the message was sent, like this: sage to the conversation box and then make sure the history box is scrolled down far enough to read the newest message. That completes the device app. If you had different e-mail accounts on two different devices and set them up with the right channel names, this app would be all you would need to have an IM chat between devices. But let’s continue assuming one device with one desktop counterpart and just one e-mail address. Add a new WPF application project to your solution. If you don’t see that as an option, check that the dropdown in the upper-right void sendMenuItem_Click(object sender, EventArgs e) { corner of the Add New Project dialog box is set to .NET Framework messaging.SendMessage(toTextBox.Text, messageTextBox.Text); 3.5. A Windows Forms Application would work equally well, but, messageTextBox.Text = “”; for these examples, we’ll use the newer technology. Set the project } That was easy. Now you just need to respond to incoming mes- name to DesktopMessagingApp. sages by implementing the incomingMesOn the form designer, drag out the sage method (see Figure 5). The catch here same controls that you did for the device is that this callback is invoked from the application. If you haven’t used WPF bebackground thread that listens for new fore, don’t be intimidated by the change messages, but you must add text to your in appearance of the designer. Also, you conversation history textbox from the don’t need to set TextBox.Multiline for UI thread. Check the InvokeRequired the history box in WPF. property to see if you’re on a background You should end up with something thread; if so, call Invoke to call the callback looking like Figure 6. If you’d prefer to just method on the UI thread. Once on the UI enter the XAML for this form, it’s included thread, simply append the incoming mes- Figure 6 Basic UI for the Desktop App in the source code download. Mobile WCF launch2008 39 Writing the Desktop Application
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.