在 Silverlight 应用程序之间发送消息时出现问题

发布于 2024-09-11 01:58:25 字数 4863 浏览 4 评论 0 原文

有人在两个 Silverlight 应用程序之间进行通信时遇到过任何问题吗?当我尝试从一个应用程序发送消息时,收到一条错误消息:“消息无法传递给接收者。”我的发送代码如下。我正在使用 用于在 Silverlight 应用程序中实现 Windows Live ID 的示例。当我在本地运行时,我可以正常工作,但是当我发布到服务器时,我收到传递错误。

    #region Fields
    private readonly LocalMessageSender sender = new LocalMessageSender("LiveIdAuthentication");
    private int attempts = 0;
    private const int MAX_ATTEMPTS = 10;
    #endregion

    #region Constructors
    public MainPage()
    {
        InitializeComponent();

        this.sender.SendCompleted += new EventHandler<SendCompletedEventArgs>(Sender_SendCompleted);
        this.SendMessage("authenticated");
    }
    #endregion

    #region Event Handlers
    private void Sender_SendCompleted(object sender, SendCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            if (attempts > MAX_ATTEMPTS)
            {
                MessageBox.Show(e.Error.Message);
                CloseWindow();
            }
            else
            {
                SendMessage("authenticated");
            }
        }
        else
        {
            attempts = 0;
            CloseWindow();
        }
    }
    #endregion

    #region Methods
    private void SendMessage(string message)
    {
        attempts++;
        this.sender.SendAsync(message);
    }

    private void CloseWindow()
    {
        HtmlPage.Window.Eval("window.open(\"about:blank\", \"_self\")");
        HtmlPage.Window.Eval("window.close()");
    }
    #endregion

抱歉忘记了接收器。这主要来自 Live ID 示例。

        private readonly WindowsLiveIdAuthentication _service;
        private readonly AsyncCallback _asyncCallback;
        private readonly object _asyncState;
        private readonly LocalMessageReceiver _receiver = new LocalMessageReceiver("LiveIdAuthentication");
        private bool _isCompleted;
        private LoadUserResult _result;

        #region Constructors
        public LoginAsyncResult(WindowsLiveIdAuthentication service, AsyncCallback asyncCallback, object asyncState)
        {
            this._service = service;
            this._asyncCallback = asyncCallback;
            this._asyncState = asyncState;
            this._receiver.MessageReceived += this.LocalMessageReceived;
        }
        #endregion

        #region Properties
        public object AsyncState
        {
            get { return this._asyncState; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return this._isCompleted; }
        }

        public LoadUserResult Result
        {
            get { return this._result; }
        }
        #endregion

        #region Methods
        public void Cancel()
        {
            if (!this._isCompleted)
            {
                this._isCompleted = true;
            }
        }

        public void Complete()
        {
            if (!this._isCompleted)
            {
                this._isCompleted = true;
                this._receiver.Dispose();

                Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
                {
                    if (this._asyncCallback != null)
                    {
                        this._asyncCallback(this);
                    }
                });
            }
        }
        #endregion

        #region Event Handlers
        public void HandleLoadCallback(IAsyncResult asyncResult)
        {
            this._result = this._service.EndLoadUser(asyncResult);
            if (!this._result.User.Identity.IsAuthenticated && !this._isCompleted && (this != asyncResult.AsyncState))
            {
                this._receiver.Listen();
            }
            else
            {
                this.Complete();
                if (Globals.CurrentUser == null)
                {
                    Globals.CurrentUser = _result.User as User;
                    Globals.SelectedDate = DateTime.Now;
                    (App.Current.RootVisual as MainPage).SetTheme(Globals.CurrentUser.CurrentTheme);
                    HtmlPage.Window.Navigate(new Uri("#UserHome", UriKind.Relative));
                }
            }
        }

        private void LocalMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            this._service.BeginLoadUser(this.HandleLoadCallback, this);
        }
        #endregion

更新: 好的,我发现 RIA 服务调用失败,导致没有调用receiver.Listen()。所以发送者没有接收者来发送消息。我仍在处理失败的 RIA 服务调用,但这是另一个问题。我会将其标记为已回答。

Has anyone had any issues communicating between two Silverlight apps. When I try to send a message from one app, I get an error, "The message could not be delivered to receiver." My code for sending is below. I'm using the similar code that is in the samples for implementing Windows Live ID in a Silverlight App. I have this working when I'm running locally, but when I post to the server, I'm getting the delivery error.

    #region Fields
    private readonly LocalMessageSender sender = new LocalMessageSender("LiveIdAuthentication");
    private int attempts = 0;
    private const int MAX_ATTEMPTS = 10;
    #endregion

    #region Constructors
    public MainPage()
    {
        InitializeComponent();

        this.sender.SendCompleted += new EventHandler<SendCompletedEventArgs>(Sender_SendCompleted);
        this.SendMessage("authenticated");
    }
    #endregion

    #region Event Handlers
    private void Sender_SendCompleted(object sender, SendCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            if (attempts > MAX_ATTEMPTS)
            {
                MessageBox.Show(e.Error.Message);
                CloseWindow();
            }
            else
            {
                SendMessage("authenticated");
            }
        }
        else
        {
            attempts = 0;
            CloseWindow();
        }
    }
    #endregion

    #region Methods
    private void SendMessage(string message)
    {
        attempts++;
        this.sender.SendAsync(message);
    }

    private void CloseWindow()
    {
        HtmlPage.Window.Eval("window.open(\"about:blank\", \"_self\")");
        HtmlPage.Window.Eval("window.close()");
    }
    #endregion

Sorry about forgetting the receiver. This is mostly from the Live ID example.

        private readonly WindowsLiveIdAuthentication _service;
        private readonly AsyncCallback _asyncCallback;
        private readonly object _asyncState;
        private readonly LocalMessageReceiver _receiver = new LocalMessageReceiver("LiveIdAuthentication");
        private bool _isCompleted;
        private LoadUserResult _result;

        #region Constructors
        public LoginAsyncResult(WindowsLiveIdAuthentication service, AsyncCallback asyncCallback, object asyncState)
        {
            this._service = service;
            this._asyncCallback = asyncCallback;
            this._asyncState = asyncState;
            this._receiver.MessageReceived += this.LocalMessageReceived;
        }
        #endregion

        #region Properties
        public object AsyncState
        {
            get { return this._asyncState; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return this._isCompleted; }
        }

        public LoadUserResult Result
        {
            get { return this._result; }
        }
        #endregion

        #region Methods
        public void Cancel()
        {
            if (!this._isCompleted)
            {
                this._isCompleted = true;
            }
        }

        public void Complete()
        {
            if (!this._isCompleted)
            {
                this._isCompleted = true;
                this._receiver.Dispose();

                Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
                {
                    if (this._asyncCallback != null)
                    {
                        this._asyncCallback(this);
                    }
                });
            }
        }
        #endregion

        #region Event Handlers
        public void HandleLoadCallback(IAsyncResult asyncResult)
        {
            this._result = this._service.EndLoadUser(asyncResult);
            if (!this._result.User.Identity.IsAuthenticated && !this._isCompleted && (this != asyncResult.AsyncState))
            {
                this._receiver.Listen();
            }
            else
            {
                this.Complete();
                if (Globals.CurrentUser == null)
                {
                    Globals.CurrentUser = _result.User as User;
                    Globals.SelectedDate = DateTime.Now;
                    (App.Current.RootVisual as MainPage).SetTheme(Globals.CurrentUser.CurrentTheme);
                    HtmlPage.Window.Navigate(new Uri("#UserHome", UriKind.Relative));
                }
            }
        }

        private void LocalMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            this._service.BeginLoadUser(this.HandleLoadCallback, this);
        }
        #endregion

UPDATE:
OK, I found out that a RIA service call had failed, which resulted in not calling receiver.Listen(). So there wasn't a receiver for the sender to send messages. I'm still working on the failed RIA service call, but that's a different issue. I'll mark this as answered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-09-18 01:58:25

另请添加接收器的代码。这是其中非常重要的一部分。例如,如果接收器不存在,那么它将失败。

Please also add the code for the receiver. That is a very important part of this. If the receiver doesn't exist, for example, then it will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文