Introduction
This article is a free continuation of How to Receive Messages from a Desktop Application and How to Send Message to a Desktop Application where the Silverlight application communicates with the standalone .NET application via TCP. In this article, I would like to show how to implement the communication via HTTP.
The example below implements a .NET application as a service and a Silverlight application as a client. The service listens to text messages and responses with their length. The client then uses the service to get the length of the given text.
The implementation uses the Eneter Messaging Framework. (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.)
Policy Server
Like the TCP communication, the HTTP communication also requires the policy XML file. Silverlight automatically requests this file when the application wants to connect to other than the site of origin. In the case of HTTP, Silverlight requests the policy file on the root of the HTTP request, i.e., if the Silverlight application requests http://127.0.0.1/MyHttpService/, then Silverlight asks for the policy server from the root: http://127.0.0.1/clientaccesspolicy.xml. If the policy file is not available or its content does not allow the communication, the HTTP request is not performed.
Service Application
The service application provides the HTTP policy service for the communication with the Silverlight client. Then it implements the simple service calculating the length for the given text. Please notice, to run the HTTP listening application, you must execute it under sufficient user rights. Otherwise, you will get an exception.
The whole implementation is very simple.
using System;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
namespace LengthService
{
class Program
{
static private IDuplexTypedMessageReceiver<int, string> myReceiver;
static void Main(string[] args)
{
HttpPolicyServer aPolicyServer = new HttpPolicyServer("http://127.0.0.1:8034/");
aPolicyServer.StartPolicyServer();
Console.WriteLine("Http Policy Server is running.");
IMessagingSystemFactory aMessaging = new HttpMessagingSystemFactory();
IDuplexInputChannel anInputChannel =
aMessaging.CreateDuplexInputChannel("http://127.0.0.1:8034/MyService/");
IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
myReceiver = aSenderFactory.CreateDuplexTypedMessageReceiver<int, string>();
myReceiver.MessageReceived += OnMessageReceived;
Console.WriteLine("The service is listening to Http.");
Console.WriteLine("Press Enter to stop the service.\n");
myReceiver.AttachDuplexInputChannel(anInputChannel);
Console.ReadLine();
myReceiver.DetachDuplexInputChannel();
aPolicyServer.StopPolicyServer();
}
static private void OnMessageReceived(object sender,
TypedRequestReceivedEventArgs<string> e)
{
if (e.ReceivingError == null)
{
int aLength = e.RequestMessage.Length;
Console.WriteLine("Received string: {0}; Responded length: {1}",
e.RequestMessage, aLength);
myReceiver.SendResponseMessage(e.ResponseReceiverId, aLength);
}
}
}
}
Silverlight Client Application
The client application uses the service to get the length of the given text. The client application uses the assembly built for Silverlight, Eneter.Messaging.Framework.Silverlight.dll. The implementation is very simple.
using System.Windows;
using System.Windows.Controls;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
namespace LengthClient
{
public partial class MainPage : UserControl
{
private IDuplexTypedMessageSender<int, string> mySender;
public MainPage()
{
InitializeComponent();
OpenConnection();
}
private void OpenConnection()
{
IMessagingSystemFactory aMessaging = new HttpMessagingSystemFactory();
IDuplexOutputChannel anOutputChannel =
aMessaging.CreateDuplexOutputChannel(
"http://127.0.0.1:8034/MyService/");
IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
mySender = aSenderFactory.CreateDuplexTypedMessageSender<int, string>();
mySender.ResponseReceived += OnResponseReceived;
mySender.AttachDuplexOutputChannel(anOutputChannel);
}
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
mySender.DetachDuplexOutputChannel();
}
private void GetLengthButton_Click(object sender, RoutedEventArgs e)
{
mySender.SendRequestMessage(UserText.Text);
}
private void OnResponseReceived(object sender,
TypedResponseReceivedEventArgs<int> e)
{
if (e.ReceivingError == null)
{
int aResult = e.ResponseMessage;
ReceivedLength.Text = aResult.ToString();
}
}
}
}
And here are the communicating applications:
I hope you found the article useful.