开发者

How to call two WCF services in my silverlight application?

I have two WCF services:

  1. abc.svc
  2. xyz.svc

I have added these to my Silverlight application successfully.

I am calling WCF services开发者_StackOverflow社区 as shown below:

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);




}

I implemented my methods, but I am not getting any data.

I am new to WCF & Silverlight. Can we call two WCF services as shown in the above code?


Cody,

There is no reason why you can't use more than one service at a time. Use this code as a guide.

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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜