LocalMessageReceiver Constructor (String, ReceiverNameScope, IEnumerable<String>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the LocalMessageReceiver class and configures it with the specified name, namescope requirement, and allowed sender domains.

Namespace:  System.Windows.Messaging
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Sub New ( _
    receiverName As String, _
    nameScope As ReceiverNameScope, _
    allowedSenderDomains As IEnumerable(Of String) _
)
public LocalMessageReceiver(
    string receiverName,
    ReceiverNameScope nameScope,
    IEnumerable<string> allowedSenderDomains
)

Parameters

  • receiverName
    Type: System.String
    The name of the receiver, which must be unique either within the global namescope or the receiver's domain, depending on the value of the nameScope parameter.

Exceptions

Exception Condition
ArgumentNullException

receiverName is nulla null reference (Nothing in Visual Basic).

-or-

allowedSenderDomains is nulla null reference (Nothing in Visual Basic).

-or-

allowedSenderDomains contains one or more nulla null reference (Nothing in Visual Basic) entries.

ArgumentException

receiverName is longer than 256 characters.

-or-

allowedSenderDomains contains one or more entries longer than 256 characters.

-or-

allowedSenderDomains contains one or more entries with invalid characters ("," and ":").

Remarks

This constructor enables you to initialize the ReceiverName, NameScope, and AllowedSenderDomains properties.

For more information, see Communication Between Local Silverlight-Based Applications.

Examples

The following code example demonstrates how to use this constructor. This example is part of a larger example available in How to: Implement Communication Between Local Silverlight-Based Applications.

Imports System
Imports System.Windows.Controls
Imports System.Windows.Messaging

Partial Public Class Receiver
    Inherits UserControl

    Public Sub New()

        InitializeComponent()

        Dim messageReceiver As New LocalMessageReceiver("receiver", _
            ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain)
        AddHandler messageReceiver.MessageReceived, _
            AddressOf messageReceiver_MessageReceived

        Try

            messageReceiver.Listen()

        Catch ex As ListenFailedException

            output.Text = "Cannot receive messages." & Environment.NewLine & _
                "There is already a receiver with the name 'receiver'."

        End Try

    End Sub

    Private Sub messageReceiver_MessageReceived( _
        ByVal sender As Object, ByVal e As MessageReceivedEventArgs)

        e.Response = "response to " & e.Message
        output.Text = _
            "Message: " & e.Message & Environment.NewLine & _
            "NameScope: " & e.NameScope.ToString() & Environment.NewLine & _
            "ReceiverName: " & e.ReceiverName & Environment.NewLine & _
            "SenderDomain: " & e.SenderDomain & Environment.NewLine & _
            "Response: " & e.Response

    End Sub

End Class
using System;
using System.Windows.Controls;
using System.Windows.Messaging;

namespace ReceivingApplication
{
    public partial class Receiver : UserControl
    {
        public Receiver()
        {
            InitializeComponent();

            LocalMessageReceiver messageReceiver =
                new LocalMessageReceiver("receiver",
                ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
            messageReceiver.MessageReceived += messageReceiver_MessageReceived;
            try
            {
                messageReceiver.Listen();
            }
            catch (ListenFailedException)
            {
                output.Text = "Cannot receive messages." + Environment.NewLine +
                    "There is already a receiver with the name 'receiver'.";
            }
        }

        private void messageReceiver_MessageReceived(
            object sender, MessageReceivedEventArgs e)
        {
            e.Response = "response to " + e.Message;
            output.Text =
                "Message: " + e.Message + Environment.NewLine +
                "NameScope: " + e.NameScope + Environment.NewLine +
                "ReceiverName: " + e.ReceiverName + Environment.NewLine +
                "SenderDomain: " + e.SenderDomain + Environment.NewLine +
                "Response: " + e.Response;
        }
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.