Thursday, June 16, 2011

Silverlight: How to Communicate with Desktop Application via HTTP

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. (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.)


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.

SilverlightDuplexHTTPConnection.png


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 implementations is very simple.

using System;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
namespace LengthService
{
    class Program
    {
        // Receiver receiving messages of type string and responding int.
        static private IDuplexTypedMessageReceiver<int, string> myReceiver;
        static void Main(string[] args)
        {
            // Start Http Policy Server
            // Note: Http policy server must be initialized to the root.
            HttpPolicyServer aPolicyServer = new HttpPolicyServer("http://127.0.0.1:8034/");
            aPolicyServer.StartPolicyServer();
            Console.WriteLine("Http Policy Server is running.");
            // Create Http messaging.
            IMessagingSystemFactory aMessaging = new HttpMessagingSystemFactory();
            // Create the input channel that is able to
            // receive messages and send back response messages.
            IDuplexInputChannel anInputChannel = 
                aMessaging.CreateDuplexInputChannel("http://127.0.0.1:8034/MyService/");


Read more: Codeproject