2011-02-25 47 views
0

我有兩個WCF服務:如何在我的Silverlight應用程序中調用兩個WCF服務?

  1. abc.svc
  2. xyz.svc

我已經成功地將這些我的Silverlight應用程序。

我打電話WCF服務,如下圖所示:

private void LoadData(DateTime dt1, DateTime dt2, string str) 

{ 

---- 

private int requestId = 0; 

---- 

Uri service = new Uri(Application.Current.Host.Source, "../ALBLSalesDataService.svc");  Uri service2 = new Uri(Application.Current.Host.Source, "../ALBLTargetDataService.svc");  ALBLSalesDataServiceClient oSoapClient = new ALBLSalesDataServiceClient("CustomBinding_ALBLSalesDataService", service.AbsoluteUri);  ALBLTargetDataServiceClient oSoapClient2 = new ALBLTargetDataServiceClient("CustomBinding_ALBLTargetDataService", service2.AbsoluteUri); 
Uri service = new Uri(Application.Current.Host.Source, "../abc.svc"); 

     Uri service2 = new Uri(Application.Current.Host.Source, "../xyz.svc"); 

     abcClient oSoapClient = new abcClient("CustomBinding_abc", service.AbsoluteUri); 

    xyzClient oSoapClient2 = new xyzClient("CustomBinding_xyz", service2.AbsoluteUri); 




oSoapClient.GetDataCompleted += oSoapClient_GetDataCompleted; 

     oSoapClient.GetDataAsync(new DateRange(dt1,dt2), new name(str), ++requestId); 



     oSoapClient2.GetDashboardTargetCompleted += (oSoapClient2_GetDashboardTargetCompleted); 

     oSoapClient2.GetDashboardTargetAsync(new DateRangee(dt1,dt2), new name(str), ++requestId); 




} 

我實現我的方法,但我沒有得到任何數據。

我是WCF新手& Silverlight。我們可以如上面的代碼所示調用兩個WCF服務嗎?

+0

您定義了兩次'service'變量,並且您似乎在行上還有其他虛假內容。你可以編輯代碼,至少看起來應該工作嗎?你也可以定義你的實際問題是什麼? – AnthonyWJones

回答

0

科迪,

沒有理由爲什麼你不能同時使用一個以上的服務。使用此代碼作爲指導。

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     DateTime dt1 = DateTime.Now; 
     DateTime dt2 = DateTime.Now; 
     string str; 

     // setup of Cody's oSoapClient2 code omitted 
     // .... 
     // setup auth service client 
     AuthenticationServiceClient client = new AuthenticationServiceClient(); 

     // setup oSoapClient2 and client "completed" event handlers 
     oSoapClient2.GetDashboardTargetCompleted += (oSoapClient2_GetDashboardTargetCompleted); 
     client.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(client_LoginCompleted); 

     // call each service asyncronusly 
     client.LoginAsync("username", "password", "", true, null); 
     oSoapClient2.GetDashboardTargetAsync(new DateRangee(dt1,dt2), new name(str), ++requestId) 
    } 

    void oSoapClient2_GetDashboardTargetCompleted(object sender, LoginCompletedEventArgs e) 
    { 
     // Do something when oServiceClient2 GetDashboardTarget call completes 
     if (e.Result != null) 
     { 
      // Do Something 
     } 
    } 

    void client_LoginCompleted(object sender, LoginCompletedEventArgs e) 
    { 
     // do something when client Login service completes 
     if (e.Error != null) 
     { 
      ErrorWindow eWindow = new ErrorWindow("Login Failed", "Error: " + e.Error); 
      eWindow.Show(); 
      return; 
     } 

     if (e.Result == false) 
     { 
      ErrorWindow eWindow = new ErrorWindow("Login Failed", "Error: Bad Username/Password combination."); 
      eWindow.Show(); 
      return; 
     } 

     // Login was successful 
     SuccessWindow success = new PopupWindow("Login Successful"); 
     success.Show(); 
    } 
相關問題