2010-07-22 94 views
0

有沒有人在兩個Silverlight應用程序之間有任何問題進行通信。當我嘗試從一個應用程序發送消息時,出現錯誤,「消息無法傳送給接收方。」我的發送代碼如下。我正在使用samples中的類似代碼在Silverlight應用程序中實現Windows Live ID。當我在本地運行時,我有這個工作,但是當我發佈到服務器時,出現傳遞錯誤。在Silverlight應用程序之間發送消息的問題

#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 

UPDATE: OK,我發現了一個RIA服務調用失敗了,這就造成了不調用receiver.Listen()。所以沒有發送者發送消息的接收者。我仍在處理失敗的RIA服務電話,但這是一個不同的問題。我會將其標記爲已回答。

回答

0

請同時添加接收器的代碼。這是這個非常重要的一部分。例如,如果接收器不存在,則它將失敗。

+0

如上所述,接收器不存在。謝謝你給我提供接收端的線索。 – 2010-07-22 21:15:42

相關問題