如何在我的silverlight应用程序中调用两个WCF服务?

时间:2011-02-25 06:42:02

标签: silverlight wcf

我有两个WCF服务:

  1. abc.svc
  2. xyz.svc
  3. 我已成功将这些添加到我的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服务,如上面的代码所示吗?

1 个答案:

答案 0 :(得分: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();
    }