Click here to Skip to main content
15,946,342 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 110.5K   56   52
Communicate Between Two Local Silverlight Applications

Introduction

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:

XML
<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
        <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"/>
    </StackPanel>
</Grid>

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:

C#
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:

C#
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.Global);
    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:

C#
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.

C#
void MainPageLoaded(object sender, RoutedEventArgs e)
{
    var localMessageReceiver = new LocalMessageReceiver("myMessagingDemo", 
                                                         ReceiverNameScope.Global, 
                                                         LocalMessageReceiver.AnyDomain);
    localMessageReceiver.MessageReceived += LocalMessageReceiverMessageReceived;
    localMessageReceiver.Listen();
}

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:

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

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

Demo

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.

History

  • 26th July, 2011: Initial post

License

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 (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 5 Pin
Carsten V2.04-Feb-13 5:45
Carsten V2.04-Feb-13 5:45 
GeneralMy vote of 5 Pin
Christian Amado28-Jul-12 13:09
professionalChristian Amado28-Jul-12 13:09 
GeneralMy vote of 5 Pin
Uday P.Singh5-Dec-11 20:59
Uday P.Singh5-Dec-11 20:59 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»5-Dec-11 21:11
professionalKunal Chowdhury «IN»5-Dec-11 21:11 
QuestionDiff Domains Pin
Jayesh Modha22-Aug-11 11:28
Jayesh Modha22-Aug-11 11:28 
AnswerRe: Diff Domains Pin
Kunal Chowdhury «IN»22-Aug-11 16:32
professionalKunal Chowdhury «IN»22-Aug-11 16:32 
GeneralMy vote of 5 Pin
Durgaprasad Budhwani16-Aug-11 1:47
Durgaprasad Budhwani16-Aug-11 1:47 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»16-Aug-11 4:09
professionalKunal Chowdhury «IN»16-Aug-11 4:09 
GeneralMy vote of 5 Pin
thatraja8-Aug-11 17:00
professionalthatraja8-Aug-11 17:00 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Aug-11 17:48
professionalKunal Chowdhury «IN»8-Aug-11 17:48 
GeneralRe: My vote of 5 Pin
thatraja8-Aug-11 17:56
professionalthatraja8-Aug-11 17:56 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Aug-11 18:03
professionalKunal Chowdhury «IN»8-Aug-11 18:03 
GeneralMy vote of 5 Pin
Kanasz Robert7-Aug-11 21:48
professionalKanasz Robert7-Aug-11 21:48 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Aug-11 1:19
professionalKunal Chowdhury «IN»8-Aug-11 1:19 
GeneralMy vote of 5 Pin
Filip D'haene6-Aug-11 5:40
Filip D'haene6-Aug-11 5:40 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Aug-11 1:19
professionalKunal Chowdhury «IN»8-Aug-11 1:19 
GeneralMy vote of 5 Pin
eshaq6-Aug-11 3:50
eshaq6-Aug-11 3:50 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Aug-11 1:18
professionalKunal Chowdhury «IN»8-Aug-11 1:18 
QuestionSuggestion for a future article Pin
dbrenth2-Aug-11 3:42
dbrenth2-Aug-11 3:42 
GeneralMy vote of 5 Pin
shakti53851-Aug-11 22:41
shakti53851-Aug-11 22:41 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»4-Aug-11 1:44
professionalKunal Chowdhury «IN»4-Aug-11 1:44 
GeneralMixed feelings Pin
Mike Klimentiev1-Aug-11 11:42
Mike Klimentiev1-Aug-11 11:42 
GeneralRe: Mixed feelings Pin
Kunal Chowdhury «IN»1-Aug-11 14:36
professionalKunal Chowdhury «IN»1-Aug-11 14:36 
GeneralRe: Mixed feelings Pin
Mike Klimentiev2-Aug-11 13:49
Mike Klimentiev2-Aug-11 13:49 
GeneralRe: Mixed feelings Pin
Paul Conrad5-Aug-11 11:28
professionalPaul Conrad5-Aug-11 11:28 

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.