Search This Blog

Sunday, March 27, 2011

Windows Phone 7: How to Communicate with Desktop Application

Summary: Simple example showing how to implement the communication from Windows Phone 7 to the standalone .NET application with using HTTP.


The example bellow implements a simple .NET application as a service and Windows Phone 7 application as a client. The service calculates two numbers and returns the result. The Windows Phone 7 client then uses the service to perform the calculation and displays the result.

It is typical for the cell-phone, that the network connection does not have to be stable. The signal can get weak or can be temporarily lost.
The good application should be able to handle this situation and should not stop to work.

The example bellow uses Eneter Messaging Framework, that allows to setup the communication that detects disconnections, automatically tries to reconnect and meanwhile stores sent messages in the buffer. Then, when the connection is available again, the messages stored in the buffer are sent to the receiver.

(Full, not limited and for non-commercial usage free version of the framework can be downloaded from http://www.eneter.net. The online help for developers can be found at http://www.eneter.net/OnlineHelp/EneterMessagingFramework/Index.html.)

Service Application
The service .NET application is responsible for the calculation of two numbers and for the responding of results. The whole implementation is very simple.
Please notice, to run the HTTP listening application you must execute it under sufficient user rights. Otherwise the application will run, but will not listen. (The framework logs the Access denied error on the debug port.)
For the debugging purposes, I recommend to execute it under administrator rights.


using System;
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.Composites;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;

namespace CalculatorService
{
    // The message transferring numbers to be calculated.
    public class RequestMsg
    {
        public int Number1;
        public int Number2;
    }

    class Program
    {
        // Receiver receiving requests to calculate two numbers and responding the result.
        static private IDuplexTypedMessageReceiver<int, RequestMsg> myReceiver;

        static void Main(string[] args)
        {
            // Create Http messaging.
            IMessagingSystemFactory anUnderlyingMessaging = new HttpMessagingSystemFactory();

            // The network can be unstable. Therefore, let's use the buffered messaging monitoring the connection
            // and with automatic reconnect in case of the disconnection.
            // If the client does not ping longer than 5 seconds, the client is considered to be disconnected
            // and its response messages are stored in the buffer.
            // If the connection occurs within 15 seconds, the response messages are sent to the client.
            IMessagingSystemFactory aBufferedMessaging = new BufferedMonitoredMessagingFactory(
                anUnderlyingMessaging,
                new XmlStringSerializer(),
                TimeSpan.FromMilliseconds(15000),   // maximum time, the client can be offline
                TimeSpan.FromMilliseconds(500),     // 'ping' frequency - this is not applicable for the receiver
                TimeSpan.FromMilliseconds(5000)     // maximum time, the 'ping' message does not have to be received
                );


            IDuplexInputChannel anInputChannel = aBufferedMessaging.CreateDuplexInputChannel("http://127.0.0.1:8034/Calculator/");

            // Create message receiver - response sender.
            IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
            myReceiver = aSenderFactory.CreateDuplexTypedMessageReceiver<int, RequestMsg>();
            myReceiver.MessageReceived += OnMessageReceived;

            Console.WriteLine("Calculator service is listening to Http. Press enter to stop listening ...");

            // Attach the duplex input channel and start listening
            // on the specified address.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.ReadLine();

            myReceiver.DetachDuplexInputChannel();
        }

        // The method is called when a request is received.
        static private void OnMessageReceived(object sender, TypedRequestReceivedEventArgs<RequestMsg> e)
        {
            // Calculate incoming 2 numbers.
            if (e.ReceivingError == null)
            {
                int aResult = e.RequestMessage.Number1 + e.RequestMessage.Number2;

                Console.WriteLine("{0} + {1} = {2}", e.RequestMessage.Number1, e.RequestMessage.Number2, aResult);

                // Send the result back.
                myReceiver.SendResponseMessage(e.ResponseReceiverId, aResult);
            }
        }
    }
}

Client Application
The Windows Phone 7 client application sends requests to calculate two numbers and displays results.
If the connection is lost, the messages are buffered and messaging system tries to automatically reconnect.
Then, if reconnected, the messages stored in the buffer are sent to the receiver.
(Notice, if you run the application from the Visual Studio, do not forget to set, that the application shall be deployed on 'Windows Phone 7 Emulator'.)
The client application uses the assembly built for the Windows Phone 7,  Eneter.Messaging.Framework.Phone.dll.


using System;
using System.Windows;
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.Composites;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Microsoft.Phone.Controls;

namespace Phone7Sender
{
    public partial class MainPage : PhoneApplicationPage
    {
        // The message transferring numbers to be calculated.
        public class RequestMsg
        {
            public int Number1;
            public int Number2;
        }

        // Sender sending the request to calculate two numbers and receiving the result.
        private IDuplexTypedMessageSender<int, RequestMsg> mySender;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            OpenConnection();
        }

        private void OpenConnection()
        {
            // Create Http messaging.
            // Note: The default constructor routes received response messages into the Silverlight thread.
            //       If it is not desired, then it can be changed.
            IMessagingSystemFactory anUnderlyingMessaging = new HttpMessagingSystemFactory();

            // The cell-phone connection can be unstable.
            // Therefore, let's use the buffered messaging monitoring the connection
            // and with automatic reconnect in case of the disconnection.
            // Create buffered messaging, that will be able to work offline 1 minute.
            // During the offline time, the sent messages are stored in the buffer and the framework tries
            // to reconnect.
            IMessagingSystemFactory aBufferedMessaging = new BufferedMonitoredMessagingFactory(
                anUnderlyingMessaging, 
                new XmlStringSerializer(), 
                TimeSpan.FromMinutes(1),            // maximum offline time
                TimeSpan.FromMilliseconds(500),     // how often the 'ping' checking the connection is invoked
                TimeSpan.FromMilliseconds(1000)     // maximum time, the response for the 'ping' shall be received
                );
            IDuplexOutputChannel anOutputChannel = aBufferedMessaging.CreateDuplexOutputChannel("http://127.0.0.1:8034/Calculator/");

            // Create message sender - response receiver.
            IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
            mySender = aSenderFactory.CreateDuplexTypedMessageSender<int, RequestMsg>();
            mySender.ResponseReceived += OnResponseReceived;

            // Attach duplex output channel and be able to send messages and receive response messages.
            mySender.AttachDuplexOutputChannel(anOutputChannel);
        }

        private void LayoutRoot_Unloaded(object sender, RoutedEventArgs e)
        {
            mySender.DetachDuplexOutputChannel();
        }

        private void SendButton_Click(object sender, RoutedEventArgs e)
        {
            // Create the request message.
            RequestMsg aMessage = new RequestMsg();
            aMessage.Number1 = int.Parse(Number1TextBox.Text);
            aMessage.Number2 = int.Parse(Number2TextBox.Text);

            // Send the message.
            // Note: If the connection is not available, the message will be stored in the buffer.
            //       We have set, the application can work offline maximum 1 minute.
            mySender.SendRequestMessage(aMessage);
        }

        private void OnResponseReceived(object sender, TypedResponseReceivedEventArgs<int> e)
        {
            if (e.ReceivingError == null)
            {
                // The response message was routed to the Silverlight thread.
                // Therefore, the value can be directly written to the UI control.
                ResultTextBox.Text = e.ResponseMessage.ToString();
            }
        }

    }
}


The following picture shows the communicating applications:

I hope, you found the article useful.

No comments:

Post a Comment