Thursday, June 16, 2011

Silverlight: Comunication between Silverlight Applications with Typed Messages

Silverlight is a nice technology with many useful features to develop cool web-applications. But if you want to communicate between Silverlight applications the possibilities are very limited. Silverlight supports only string messages with limited size (max 40 kilobytes long). If the communication scenario is very simple this basic possibilities can be sufficient. But if your communication needs to recognize between different messages, or you need to communicate request-response with different message types, or you just need messages bigger than 40 KB then the basic Silverlight messaging is not sufficient and you must implement the whole communication "plumbing" in yourself.

Eneter.Messaging.Framework provides a lot of possibilities for the communication between Silverlight applications. For the beginning, I would like to show how to use the framework to overcome the known limitations of Silverlight.

The example bellow shows how to send messages of desired type from one Silverlight application to another.

1. Add Eneter.Messaging.Framework.Silverlight.dll into your Silverlight applications references to use the messaging functionality. (Full, not limited and for non-commercial usage free version of the framework can be downloaded from www.eneter.net.)

2. Implement Silverlight application receiving typed messages.
The code with using the framework is very simple:

using System.Collections.ObjectModel;
using System.Windows.Controls;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.SilverlightMessagingSystem;
namespace StrongTypeReceiver
{
    public partial class MainPage : UserControl
    {
        // Data type to be sent as the message.
        public class BasketItem
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public int Price { get; set; }
        }
        public MainPage()
        {
            InitializeComponent();
            // Initialize your UI.
            BasketItems.ItemsSource = myBasketItems;
            // Create input channel to receive message from Silverlight.
            // Note: The channel id (in our case BasketId) represents the address of the receiver.
            IMessagingSystemFactory aMessagingFactory = new SilverlightMessagingSystemFactory();
            IInputChannel anInputChannel = aMessagingFactory.CreateInputChannel("BasketId");
            // Instantiate strong typed receiver.
            ITypedMessagesFactory aTypedMessagesFactory = new TypedMessagesFactory();
            myTypedMessageReceiver = aTypedMessagesFactory.CreateTypedMessageReceiver<BasketItem>();
            
            // Handler for received messages.
            myTypedMessageReceiver.MessageReceived += OnTypedMessageReceived;
            // Attach input channel and listen.
            myTypedMessageReceiver.AttachInputChannel(anInputChannel);
        }


Read more: Codeproject