none
LocalMessageSender in browser and LocalMessageReceiver out of browser

    Question

  • I am trying to get q LocalMessageSender in browser and LocalMessageReceiver out of browser to communicate. I've successfully made two sl controls talk to each on the same aspx page before. I've looked at this thread http://forums.silverlight.net/forums/p/175065/394082.aspx and copied the code from the referenced msdn article. And I've disabled the sender trust check on the receiver.

    Here is the relevant code from the sender:

     private LocalMessageSender messageSender;
     private const int MAX_ATTEMPTS = 10000;
     private int attempt = 1;

            public Page()
            {
                InitializeComponent();
                Loaded += new RoutedEventHandler(Page_Loaded);

                messageSender = new LocalMessageSender("ADSMessageReceiver", LocalMessageSender.Global);
                messageSender.SendCompleted += sender_SendCompleted;
            }

            void Page_Loaded(object sender, EventArgs e)
            {
                SendMessage("ADS site opened");
            }

            private void SendMessage(string message)
            {
                messageSender.SendAsync(message, attempt);
            }

            private void sender_SendCompleted(object sender, SendCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    LogError(e);
                    attempt++;
                    if (attempt > MAX_ATTEMPTS)
                    {
                        System.Windows.MessageBox.Show("Could not send message. " + e.Error.Message);
                        //return;
                    }
                    SendMessage(e.Message);
                    return;
                }

                System.Windows.MessageBox.Show("Message: " + e.Message + Environment.NewLine +
                    "Attempt " + (int)e.UserState +
                    " completed." + Environment.NewLine +
                    "Response: " + e.Response + Environment.NewLine +
                    "ReceiverName: " + e.ReceiverName + Environment.NewLine +
                    "ReceiverDomain: " + e.ReceiverDomain);

                // Reset attempt counter.
                attempt = 1;
            }

            private void LogError(SendCompletedEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(
                    "Attempt number {0}: {1}: {2}", (int)e.UserState,
                    e.Error.GetType().ToString(), e.Error.Message);
            }

    And here is the code from the receiver:

            public MainPage()
            {
                InitializeComponent();
                Loaded += new RoutedEventHandler(MainPage_Loaded);
                LocalMessageReceiver messageReceiver =
                        new LocalMessageReceiver("ADSMessageReceiver",
                        ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
                messageReceiver.MessageReceived += messageReceiver_MessageReceived;
                messageReceiver.DisableSenderTrustCheck = true;

                try
                {
                    messageReceiver.Listen();
                }
                catch (ListenFailedException)
                {
                    CreateNotification("Can not receive", "Cannot receive messages." + Environment.NewLine +
                        "There is already a receiver with the name 'ADSMessageReceiver'.");
                }
            }

            private void messageReceiver_MessageReceived(
                object sender, MessageReceivedEventArgs e)
            {
                e.Response = "response to " + e.Message;

                CreateNotification("New message", "Message: " + e.Message + Environment.NewLine +
                    "NameScope: " + e.NameScope + Environment.NewLine +
                    "ReceiverName: " + e.ReceiverName + Environment.NewLine +
                    "SenderDomain: " + e.SenderDomain + Environment.NewLine +
                    "Response: " + e.Response);
            }

    The message I get from the sender is 'The message could not be delivered to receiver'.

    I've tried running the OOB app with and without elevated trust.

    Saturday, April 17, 2010 7:02 AM

Answers

  • OK, figured out what to do. When debugging, on the debug properties tab, I had to set it as an out of browser app (duh!). When installing, I created an install page in the website where the in browser app was running. That seems to have fixed it. What I still don't understand is why this code:

    LocalMessageReceiver messageReceiver =

    new LocalMessageReceiver("ADSMessageReceiver",

    ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);

    messageReceiver.DisableSenderTrustCheck = true;

    doesn't allow it to work without installing it from the same website.

    Sunday, April 25, 2010 6:15 AM

All replies

  • Hi snelldl,

    Thanks for sharing the code. I copied it and created two solutions: one for sender and the other for receiver. The only modification is that I don't use CreateNotification because I don't have this method, I hope this doesn't matter.

    Anyway, I tested these two solutions:open receiver first and then open sender,  send/receive works well. I uploaded the two solution to here, please download it and have a try. Thanks.

    Sunday, April 18, 2010 10:25 PM
  • I see that you made both applications to run as OOB. What I am having trouble with is when the sender is IN BROWSER and the receiver is OOB.

    Monday, April 19, 2010 6:03 AM
  • OK, figured out what to do. When debugging, on the debug properties tab, I had to set it as an out of browser app (duh!). When installing, I created an install page in the website where the in browser app was running. That seems to have fixed it. What I still don't understand is why this code:

    LocalMessageReceiver messageReceiver =

    new LocalMessageReceiver("ADSMessageReceiver",

    ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);

    messageReceiver.DisableSenderTrustCheck = true;

    doesn't allow it to work without installing it from the same website.

    Sunday, April 25, 2010 6:15 AM
  • the answer is at:

    http://msdn.microsoft.com/en-us/library/dd833063(v=vs.95).aspx 

    Avoiding cross-domain issues

    When the sending and receiving applications are hosted on different domains, there may be security issues that prevent local messaging, or require additional code.

    First, local messaging is only possible between domains using the same URI scheme. For example, a sender hosted at an HTTP URI cannot send messages to a receiver hosted at an HTTPS URI.

    Additionally, in Internet Explorer, the hosts of the sending and receiving applications must reside in the same security zone. You can disable this restriction, however, by setting the LocalMessageReceiver.DisableSenderTrustCheck property to true. This is useful, for example, to establish communication between a Web-hosted application in the Internet zone and an out-of-browser application in the Trusted zone. For more information about out-of-browser applications, see Out-of-Browser Support.

    With cross-domain local messaging, you will sometimes need to take extra precautions to avoid name conflicts or message hijacking. Remember that the first application that registers a receiver name within a particular name scope will receive the messages intended for that identity. This could cause problems if an unknown application with the same identity is already open in another browser window or tab when your application is loaded.

    If you use the global name scope, you should avoid using common receiver names. If security is an issue, you should consider using domain name scoping and restrict the domains that the receiver can receive from. Finally, you should avoid sending sensitive information through a local messaging channel unencrypted.

    Tuesday, August 21, 2012 9:03 AM