Friday, April 12, 2019

Call Synchronous Web Service Operation in C# Asynchronously

When a Web Service WSDL is imported in Visual Studio, a new NetSuiteService class is generated. For each synchronous operation an asynchronous clone is created with Async prefix. For example, for search() operation, searchAsync() exists.

This is an example of how to execute searchAsync() in C#:
- Prepare a new CustomerSearchBasic object instance
- Create a callback function used for processing the results
- Assign the callback function to searchCompleted property of NetSuiteService object instance
- Call searchAsync()
- searchAsync() will exit immediately and the application can continue with execution
- The Web Services connection stays open and once the result is returned by server, callback function is called
- The callback function gets the results in EventArgs and can process them

Example code (search for Customer with Internal ID = 100):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using NSWS_SyncAsync.com.netsuite.webservices;

 

namespace NSWS_SyncAsync

{

    class Program

    {

        static void searchCompleted(object sender, EventArgs e)

        {

            // Get results from the EventArgs object

            searchCompletedEventArgs args = (searchCompletedEventArgs)e;

           

            Console.WriteLine("Callback executed.");

 

            if (args.Result.status.isSuccess)

            {

                // If search was successful, print out the results

                if (args.Result.recordList != null)

                {

                       // Print out all returned records

                        foreach (Record record in args.Result.recordList)

                        {

                            Customer customer = (Customer)record;

                            Console.WriteLine("Returned customer: "

                                + customer.companyName);

                        }

 

                }

                Console.WriteLine("Callback finished. Press ENTER to exit");

            }

            else

            {

                // If search fails, write the error message

                Console.WriteLine("Search failed");

 

                if (args.Result.status.statusDetail != null)

                {

                    foreach (StatusDetail statusDetail in args.Result.status.statusDetail)

                    {

                        Console.WriteLine("ERROR: " + statusDetail.message);

                    }

                }

            }

        }

       

        static void Main(string[] args)

        {

            // create service object and CookieContainer

            NetSuiteService service = new NetSuiteService();

            service.CookieContainer = new System.Net.CookieContainer();

 

            // create Passport with credentials

            service.passport = new Passport();

            service.passport.account = "00000";

            service.passport.email = "user@domain.com";

            service.passport.password = "password";

 

            // Example search object (looking for Customer with Internal ID = 100)

            CustomerSearchBasic csearch = new CustomerSearchBasic();

            csearch.internalId = new SearchMultiSelectField();

            csearch.internalId.@operator = SearchMultiSelectFieldOperator.anyOf;

            csearch.internalId.operatorSpecified = true;

            csearch.internalId.searchValue = new RecordRef[1];

            csearch.internalId.searchValue[0] = new RecordRef();

            csearch.internalId.searchValue[0].internalId = "100";

 

            // Assign callback function

            service.searchCompleted += new searchCompletedEventHandler(searchCompleted);

 

            // Run the search

            service.searchAsync(csearch);

            // This call exits immediately and the application

            //   will be waiting with Console.ReadLine()

 

            Console.WriteLine("Search operation finished. Once the response is "

                +"returned by server, a callback function will be executed.");

            Console.ReadLine();

        }

    }

}

No comments:

Post a Comment