Click here to Skip to main content
15,877,888 members
Articles / Programming Languages / C#

How to Communicate Between Two Local Silverlight Applications?

Rate me:
Please Sign up or sign in to vote.
4.96/5 (44 votes)
26 Jul 2011CPOL3 min read 105.5K   56   52
Communicate Between Two Local Silverlight Applications


Sometimes, it is required to communicate between two local Silverlight applications. Though this situation is very limited, it may be required as per your business deal. In such a case, the local messaging mechanism comes into the place.

In this post, we will learn about this local messaging system using a small Silverlight application where we will have a sender project and a receiver project. At the end of the article, I embedded the demo which will give you more visibility on what we are going to do in the demo. Let's discuss more about this topic.

Create Project Structure

Let's start by creating the project structure for our sample demo app. In this demo, we will create two different Silverlight projects called "Sender" and "Receiver". Sender project will broadcast some message and the receiver will catch that message and do the next operation according to that.

Here is the project structure for your reference:

Silverlight Local Messaging - Project Structure

Create the Sender UI

Let us create the UI of the sender application. Here we will have couple of radio buttons for selecting different colors and a button which enables the user to submit the message to broadcast. Here is the XAML code:

<Grid x:Name="LayoutRoot" Background="White">
        <RadioButton x:Name="rdoRed" GroupName="color" Content="Red"/>
        <RadioButton x:Name="rdoGreen" GroupName="color" Content="Green"/>
        <RadioButton x:Name="rdoBlue" GroupName="color" Content="Blue"/>
        <RadioButton x:Name="rdoYellow" GroupName="color" Content="Yellow"/>
        <Button Content="Send" Height="26" Width="150" Click="SendButtonClick"/>

This will render as below in the UI:

Silverlight Local Messaging - Sender's UI

Implement Message Broadcasting

It's time to send the message to the receiver application. To do this, there is a class called LocalMessageSender and it is present under "System.Windows.Messaging" namespace, which takes receiver name and receiver domain as constructor parameter:

public LocalMessageSender(string receiverName, string receiverDomain);

On button click, we will check which color has been selected and then we will create the instance of the class LocalMessageSender and pass proper color as message to the SendAsync() method as implemented below:

private void SendButtonClick(object sender, RoutedEventArgs e)
    var rdoRedSelected = rdoRed.IsChecked == true;
    var rdoGreenSelected = rdoGreen.IsChecked == true;
    var rdoBlueSelected = rdoBlue.IsChecked == true;
    var rdoYellowSelected = rdoYellow.IsChecked == true;
    var localMessageSender = new LocalMessageSender("myMessagingDemo", 
    localMessageSender.SendAsync(rdoRedSelected ? "Red" 
                                                : rdoGreenSelected ? "Green" 
                                                : rdoBlueSelected ? "Blue" 
                                                : rdoYellowSelected ? "Yellow"
                                                : "White");

It has completed event (SendCompleted) for the SendAsync() method where you can check ReceiverName, ReceiverDomain, Message and Response if any.

Implementation of Message Receiving

Similar to the LocalMessageSender, we have another class called LocalMessageReceiver present in the same namespace called "System.Windows.Messaging". It takes receiver name, receiver's namescope and allowed sender domains as constructor parameters as shown below:

public LocalMessageReceiver(string receiverName, 
                                   ReceiverNameScope nameScope, 
                                   IEnumerable<string> allowedSenderDomains);

In the page loaded event of receiver project, we will create the instance of the LocalMessageReceiver, register the MessageReceived event and then call the Listen() method to start listening to the message.

void MainPageLoaded(object sender, RoutedEventArgs e)
    var localMessageReceiver = new LocalMessageReceiver("myMessagingDemo", 
    localMessageReceiver.MessageReceived += LocalMessageReceiverMessageReceived;

In the message received event implementation, we will check the type of message and based on the received message, we will do proper operation. In our case, based on the color information that we received, we will change the color of the background of the LayoutRoot grid of the receiver application.

Here is the implementation of the same:

void LocalMessageReceiverMessageReceived(object sender, MessageReceivedEventArgs e)
    var message = e.Message;
    switch (message)
        case "Red":
            LayoutRoot.Background = new SolidColorBrush(Colors.Red);
        case "Green":
            LayoutRoot.Background = new SolidColorBrush(Colors.Green);
        case "Blue":
            LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
        case "Yellow":
            LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);
            LayoutRoot.Background = new SolidColorBrush(Colors.White);

That's all about the implementation part. Let's see it in action.


Let's run our application. As we have two projects, run each of them one by one. Once run, you will have two browser windows having one as Sender application and the other as Receiver application. By default, the receiver will have white background. Now go to the sender application and choose any one of the radio buttons to select the color. Then click "Send" button to send the color message to the other Silverlight application. The receiver application will catch the message based on the name and change the color of the layout accordingly.

Have a look into the demo of the same here which will give you proper visibility to the implementation:

Silverlight Local Messaging Demo

Hope this helped you to understand the local messaging system of Silverlight application. Remember that this will not work when your browser is in Private mode.


  • 26th July, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog ( and CodeProject.

Books authored:

Connect with Kunal on:

Comments and Discussions

GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»29-Jul-11 4:16
professionalKunal Chowdhury «IN»29-Jul-11 4:16 
GeneralGood info - voted 5! Pin
Nish Nishant26-Jul-11 5:56
sitebuilderNish Nishant26-Jul-11 5:56 
GeneralRe: Good info - voted 5! Pin
Kunal Chowdhury «IN»26-Jul-11 6:01
professionalKunal Chowdhury «IN»26-Jul-11 6:01 
GeneralNice! Pin
Ravi Bhavnani26-Jul-11 4:28
professionalRavi Bhavnani26-Jul-11 4:28 
AnswerRe: Nice! Pin
Nish Nishant26-Jul-11 5:56
sitebuilderNish Nishant26-Jul-11 5:56 
GeneralRe: Nice! Pin
Kunal Chowdhury «IN»26-Jul-11 6:02
professionalKunal Chowdhury «IN»26-Jul-11 6:02 
AnswerRe: Nice! Pin
Nish Nishant26-Jul-11 6:03
sitebuilderNish Nishant26-Jul-11 6:03 
GeneralRe: Nice! Pin
Ravi Bhavnani26-Jul-11 7:07
professionalRavi Bhavnani26-Jul-11 7:07 
Busterboy, have you cooked that omlette yet?  Get on with it, me lad! Big Grin | :-D

My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware

GeneralRe: Nice! Pin
Nish Nishant26-Jul-11 7:09
sitebuilderNish Nishant26-Jul-11 7:09 
GeneralRe: Nice! Pin
Ravi Bhavnani26-Jul-11 7:04
professionalRavi Bhavnani26-Jul-11 7:04 
GeneralRe: Nice! Pin
Nish Nishant26-Jul-11 7:06
sitebuilderNish Nishant26-Jul-11 7:06 
AnswerRe: Nice! Pin
Kunal Chowdhury «IN»26-Jul-11 6:00
professionalKunal Chowdhury «IN»26-Jul-11 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.