MSDN Magazine Launch Issue - February 15, 2008 - (Page 38) and the solution MsdnMessagingSample, as in ment many of the Service Model classes included Figure 1. You will be taken to another dialog box, in desktop WCF, you must use the messaging API, where you can choose your target platform and which requires a bit more code. Be sure to write .NET Compact Framework version. Choose the this code in a platform-neutral way by only callWindows Mobile 6 Professional SDK platform and ing the WCF API exposed by the .NET Compact set the .NET Compact Framework version to 3.5. If Framework so that you can share it between your you do not see an option for the Windows Mobile device and desktop applications, thus making each SDK, visit go.microsoft.com/fwlink/?LinkID=81684 and individual application small and simple. download the Windows Mobile SDK, then install Add a new class file called Messaging.cs. This it and work your way back to this step. will contain the Messaging class, which will inYou should find yourself looking at the Visual clude all the plumbing necessary to send and reStudio 2008 form designer with the Windows ceive messages. In a real application, this class may Mobile device and your empty main form disbe much larger for better thread safety and scalplayed. To write a simple chat program, drop a ability, but I’ll keep my example short to focus on few TextBox and Label controls on the form. You the task in this sample. might set TextBox.ReadOnly = True on your his- Figure 2 The Application UI I chose to make the Messaging class a generic tory textbox to prevent unintentional entry. And Messaging class where T is the type of data be sure to set TextBox.Multiline = True for the history box. Add being passed back and forth between apps (see Figure 4). In a more a Send button to the menu so it can be pressed using a hardware complex app, you could pass multiple types of objects around and button. And now for my favorite: set Form.MinimizeBox to False distinguish the messages using the message Action string, but I will so that the X in the corner turns to “ok”—this way, your app will not do that here. Any T will work for this class as long as it can be exit instead of just being hidden when you press it. In the end, you serialized using XmlSerializer. should have something that looks like Figure 2. The Messaging class will offer two methods for sending and Since you’ll be using the Compact Framework version of WCF, receiving messages. Receiving messages will involve a background you’ll need to add some assembly references. Right-click on the thread listening for messages and invoking a callback method project’s References folder within Solution Explorer and click Add whenever a message arrives. It will have to build the channel in Reference. From the .NET tab, select these assemblies: the constructor and offer a Close method to tear down the chan• System.ServiceModel.dll nel when the application closes. • System.Runtime.Serialization.dll Since there are two mail binding classes (one for desktops and • Microsoft.ServiceModel.Channels.Mail.dll one for mobile devices), this class will take the binding to use as an • Microsoft.ServiceModel.Channels.Mail.WindowsMobile.dll argument to its constructor. So that you can use the same e-mail WCF relies on any implementation of the abstract class XmlOb- account for both desktop and device without the two apps reading jectSerializer to fill the body of its messages. In the desktop version, each other’s incoming messages, you’ll need to use different chanWCF comes with DataContractSerializer, which derives from this nel names for the desktop and device—therefore, the constructor class. Compact WCF does not include DataContractSerializer, so will take these as arguments, too. instead you’ll use XmlSerializer. Since XmlSerializer does not deFigure 3 XmlObjectSerializer rive from XmlObjectSerializer as required by WCF, you can write a small class that derives from it and simply calls on XmlSerializer using System; using System.Runtime.Serialization; to do the heavy lifting. using System.Xml.Serialization; Add a new class called MessageSerializer, derived from XmlObclass MessageSerializer : XmlObjectSerializer { jectSerializer, to your project. If you right-click on the base class XmlSerializer serializer; name in your source file, Visual Studio will offer to stub out autopublic MessageSerializer(Type type) { matically all the functions you need to implement. All you need to serializer = new XmlSerializer(type); do in this class is add a few lines of code, as shown in Figure 3. They } define a constructor that initializes the XmlSerializer and implepublic override void WriteObject( ments the ReadObject and WriteObject methods. You’ll need to System.Xml.XmlDictionaryWriter writer, object graph) { serializer.Serialize(writer, graph); write in the WriteObject method yourself. } Those methods Visual Studio added to your class that I don’t list public override object ReadObject( here can simply throw NotImplementedException, as WCF will System.Xml.XmlDictionaryReader reader, not call them anyway. bool verifyObjectName) { WCF Messaging Plumbing } } return serializer.Deserialize(reader); To send and receive messages, you must initialize the WCF mail channel. Since the .NET Compact Framework 3.5 does not imple38 msdnmagazine Mobile WCF // http://go.microsoft.com/fwlink/?LinkID=81684
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.