Summary
This is a simple example showing how to implement communication from Windows Phone 7 to a standalone .NET application using HTTP.
Introduction
The example below 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 a cell-phone that the network connection does not have to be stable. The signal can get weak or can be temporarily lost. A good application should be able to handle this situation and should not stop working.
The example below uses the 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.
(The framework is free and 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 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
{
public class RequestMsg
{
public int Number1;
public int Number2;
}
class Program
{
static private IDuplexTypedMessageReceiver<int, RequestMsg> myReceiver;
static void Main(string[] args)
{
IMessagingSystemFactory anUnderlyingMessaging = new HttpMessagingSystemFactory();
IMessagingSystemFactory aBufferedMessaging =
new BufferedMonitoredMessagingFactory(
anUnderlyingMessaging,
new XmlStringSerializer(),
TimeSpan.FromMilliseconds(15000),
TimeSpan.FromMilliseconds(500),
TimeSpan.FromMilliseconds(5000)
);
IDuplexInputChannel anInputChannel =
aBufferedMessaging.CreateDuplexInputChannel(
"http://127.0.0.1:8034/Calculator/");
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 ...");
myReceiver.AttachDuplexInputChannel(anInputChannel);
Console.ReadLine();
myReceiver.DetachDuplexInputChannel();
}
static private void OnMessageReceived(object sender,
TypedRequestReceivedEventArgs<RequestMsg> e)
{
if (e.ReceivingError == null)
{
int aResult = e.RequestMessage.Number1 + e.RequestMessage.Number2;
Console.WriteLine("{0} + {1} = {2}",
e.RequestMessage.Number1, e.RequestMessage.Number2, aResult);
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 the 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 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
{
public class RequestMsg
{
public int Number1;
public int Number2;
}
private IDuplexTypedMessageSender<int, RequestMsg> mySender;
public MainPage()
{
InitializeComponent();
OpenConnection();
}
private void OpenConnection()
{
IMessagingSystemFactory anUnderlyingMessaging = new HttpMessagingSystemFactory();
IMessagingSystemFactory aBufferedMessaging =
new BufferedMonitoredMessagingFactory(
anUnderlyingMessaging,
new XmlStringSerializer(),
TimeSpan.FromMinutes(1),
TimeSpan.FromMilliseconds(500),
TimeSpan.FromMilliseconds(1000)
);
IDuplexOutputChannel anOutputChannel =
aBufferedMessaging.CreateDuplexOutputChannel(
"http://127.0.0.1:8034/Calculator/");
IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
mySender = aSenderFactory.CreateDuplexTypedMessageSender<int, RequestMsg>();
mySender.ResponseReceived += OnResponseReceived;
mySender.AttachDuplexOutputChannel(anOutputChannel);
}
private void LayoutRoot_Unloaded(object sender, RoutedEventArgs e)
{
mySender.DetachDuplexOutputChannel();
}
private void SendButton_Click(object sender, RoutedEventArgs e)
{
RequestMsg aMessage = new RequestMsg();
aMessage.Number1 = int.Parse(Number1TextBox.Text);
aMessage.Number2 = int.Parse(Number2TextBox.Text);
mySender.SendRequestMessage(aMessage);
}
private void OnResponseReceived(object sender,
TypedResponseReceivedEventArgs<int> e)
{
if (e.ReceivingError == null)
{
ResultTextBox.Text = e.ResponseMessage.ToString();
}
}
}
}
The following picture shows the communicating applications:
I hope you found the article useful.