Compartilhar via


Calling methods asynchronously

Some methods can take considerable time to process, especially when working with large data sets in Microsoft Dynamics GP. Rather than making the user wait for a lengthy service call to finish before returning control back to them, your application can call the method asynchronously. Control will be returned back to the application while the Dynamics GP service processes the request. When results are available, the event handler in your application will be notified and can act on them.

Hint: The "GetList" methods in the Dynamics GP service can have longer processing times, especially when the data set being searched is large, and the criteria is complex.

The proxy generated for the legacy endpoint always contains the asynchronous methods. The proxy generated for the native endpoint does not contain the asynchronous methods. If you want to use the asynchronous methods for the native endpoint, you must specify that they be generated when you create the service reference to connect to the native endpoint. Do this by clicking Advanced when you create the service reference, and then marking the Generate asynchronous operations option. Refer to Visual Studio 2008 and later for details about creating a service reference.

The following C# example demonstrates how the GetCustomerList method can be called asynchronously. After setting up the criteria for the method call, the example creates an event handler to receive the response of the asynchronous call. Then the asynchronous call is made. The event handler at the end of the example receives the result when the call has finished. It examines the result for any errors, and if there are none, displays the number of customers that match the criteria.

Cc508758.LegacyEndpoint(en-us,MSDN.10).gif** Legacy endpoint**

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DynamicsGPWebServiceSample.DynamicsGPService;

namespace DynamicsGPWebServiceSample
{
    class Program
    {
         static void Main(string[] args)
        {
            CompanyKey companyKey;
            Context context;
            CustomerCriteria customerCriteria;
            LikeRestrictionOfString stateRestriction;
            LikeRestrictionOfString nameRestriction;
            RestrictionOfNullableOfBoolean activeRestriction;

            // Create an instance of the service
            DynamicsGP wsDynamicsGP = new DynamicsGP();

            // Make sure that default credentials are being used
            wsDynamicsGP.UseDefaultCredentials = true;

            // Create a context with which to call the service
            context = new Context();

            // Specify which company to use (sample company)
            companyKey = new CompanyKey();
            companyKey.Id = (-1);

            // Set up the context
            context.OrganizationKey = (OrganizationKey)companyKey;

            // Create the criteria
            customerCriteria = new CustomerCriteria();

            // Specify the criteria for the customer summaries to retrieve
            stateRestriction = new LikeRestrictionOfString();
            stateRestriction.Like = "%IL%";
            customerCriteria.State = stateRestriction;

            nameRestriction = new LikeRestrictionOfString();
            nameRestriction.Like = "A%";
            customerCriteria.Name = nameRestriction;

            activeRestriction = new RestrictionOfNullableOfBoolean();
            activeRestriction.EqualValue = true;
            customerCriteria.IsActive = activeRestriction;

            // Use an event handler for the asynchronous web method
            wsDynamicsGP.GetCustomerListCompleted += new
            GetCustomerListCompletedEventHandler
            (wsDynamicsGP_GetCustomerListCompleted);

            // Retrieve the list of customer summaries (asynchronously)
            wsDynamicsGP.GetCustomerListAsync(customerCriteria, context);

            Console.WriteLine("Do additional work while waiting for result.");
            Console.ReadLine();
        }

        // The event handler that responds to the completed event
        static private void wsDynamicsGP_GetCustomerListCompleted(object
        sender, GetCustomerListCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Display the number of customers matching the criteria
                MessageBox.Show("Active IL customers beginning with 'A': " +
                e.Result.Length.ToString());
            }
        }
    }
}