Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Visual Basic

Local Messaging in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
18 Nov 2009MIT 17.6K   161   7  
Communicating with Silverlight applications locally.

Image 1

Introduction

The new Silverlight 3.0 allows plug-ins to be exposed as a listener, with a specific name, that may be addressed by other plug-in instances knowing this name. If you have ever used named-pipes for inter-process communications, you may found that Local Connections are very similar. There are two classes you may use to establish a connection: LocalMessageReceiver and LocalMessageSender.

Using the Code

Here is a sample for LocalMessageSender:

VB
Dim lms As System.Windows.Messaging.LocalMessageSender = _
  New System.Windows.Messaging.LocalMessageSender("receiveParent", _
  System.Windows.Messaging.LocalMessageSender.Global)
'you can restrict sending messages to any domain

Here is a sample for LocalMessageReceiver:

VB
Dim lmr As System.Windows.Messaging.LocalMessageReceiver = _
     New System.Windows.Messaging.LocalMessageReceiver("receiveParent", _ 
                        Messaging.ReceiverNameScope.Global,_ 
                        System.Windows.Messaging.LocalMessageReceiver.AnyDomain)
AddHandler lmr.MessageReceived,AddressOf HandleMessage
'attach message receive handler

'Listen global,receive any domain 
Try
    lmr.Listen()
Catch ex As System.Windows.Messaging.ListenFailedException
    'if there is already a receiver with the name 'receiveParent'
    'LocalMessageReceiver  throws exception
End Try

Sending a message:

VB
lms.SendAsync("Message To Send")

Receiving a message:

VB
Private Sub HandleMessage(ByVal sender as Object, _
        Byval e as ByVal e As System.Windows.Messaging.MessageReceivedEventArgs)
    'process message...
End Sub

You can send messages in XML or JSON format if your message is in a complex format.

Remember that the maximum message length is 40 KB. (Reference.)

Points of Interest

I discovered a listener use a unique receiver name. When you use a listener with the same receiver name, it throws an exception. If we want to make our application single instance on the computer, this could help us!

History

  • 2009.11.19 - First release.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) Adasoft
Turkey Turkey
I'm a software developer at Adasoft.
My specialists is on Web and Silverlight.

Especially i like wroting javascript codes(It is much harder,but gets more satisfaction Smile | :) )

You can reach my tips at my homepage(Turkish only)

Comments and Discussions

 
-- There are no messages in this forum --