none
How do I call a wcfservice asnychonously to save the return value to a list RRS feed

  • Question

  • Hi friends,

    i am learner in WCF and I have a problem with calling a wcf service function from a xaml page.I am trying to call it asynchonously from my xaml page. The function (getTableFields) returns a list of datatables that i bind to a grid view in wpf. 
    This is how i call the method.


    private void GetFields()
            {
                //get the selected table
                ExchangeTable selectedTable = comboTable.SelectedItem as ExchangeTable;
                NameValue selectedLanguage = comboSelectLanguage.SelectedItem as NameValue;
                //call the ws to get the table fields
                DatabaseManagementService.DatabaseManagementServiceClient proxy = new DatabaseManagementService.DatabaseManagementServiceClient();
                proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(Common.DatabaseManagementServiceAddress);
                proxy.GetTableFieldsCompleted += new EventHandler<DatabaseManagementService.GetTableFieldsCompletedEventArgs>(proxy_GetTableFieldsCompleted);
                proxy.GetTableFieldsAsync(selectedTable.ExchangeDbLogicalName, selectedTable.LogicalName, true, selectedLanguage.Value);
            }

    void proxy_GetTableFieldsCompleted(object sender, DatabaseManagementService.GetTableFieldsCompletedEventArgs e)
           {
               if (Common.WebServiceException(e.Error))
                   return;

               if (e.errorCode == 0)
               {
                  dataGridFields.ItemsSource = e.Result;
               }
              
           }



    Now i need to store the list of datatables that i get from the wcf method getTableFields() in another global variable and use it for my purpose. 
    How to do it ?
    For example, i need something like below:

    list of<datatables> originalfields = new list of<datatables>(); //declared globally in my xaml page 
    originalfields = GetTableFields(...);  //coming from the service  


    I tried to call GetFields() on a button click like this:

    GetFields();
    changedFields = GetChangedFields(originalfields,dataSource);


     again but the proxy_GetTableFieldsCompleted event is getting executed at the end of the button click event and hence originalfields is always null AND I get a null reference error when i try to use it. Please help me as am struck with the whole day. 

    Thanks in advance 
    • Edited by Ram Nt Wednesday, July 8, 2015 1:55 PM
    Wednesday, July 8, 2015 1:54 PM

Answers

  • How about saving it when you get it for your grid, like this:

    void proxy_GetTableFieldsCompleted(object sender, DatabaseManagementService.GetTableFieldsCompletedEventArgs e)
    {
        if (Common.WebServiceException(e.Error))
            return;
    
        if (e.errorCode == 0)
        {
           dataGridFields.ItemsSource = e.Result;
           originalfields = e.Result; // or set = dataGridFields.ItemSource instead
        }
       
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, July 9, 2015 1:34 AM
    Moderator