在Silverlight应用程序之间发送消息的问题

时间:2010-07-22 03:42:45

标签: silverlight windows-live-id

有没有人在两个Silverlight应用之间进行通信时遇到任何问题。当我尝试从一个应用程序发送消息时,我收到一条错误消息,“消息无法传递给接收方。”我的发送代码如下。我正在使用samples中的类似代码在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服务电话,但这是一个不同的问题。我会将此标记为已回答。

1 个答案:

答案 0 :(得分:0)

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