Calling object methods in SOAP web services

The SOAP protocol is designed to provide access to object's methods over the TCP connection. In the previous recipe, we published a NAV object, and now we will see how to consume the service provided by the published codeunit and call its methods.

How to do it...

In this recipe, you will create a project in Visual Studio to consume a NAV web service and call a codeunit method.

  1. Run Visual Studio. Create a new project based on the Console Application project template. In the Name field, enter the name of the project: PostCodeWebServiceClient.
  2. In the Solution Explorer window, expand the contents of the project, right-click on the References section, and choose the option Add Service Reference from the drop-down menu:

    How to do it...

  3. The Add Service Reference dialog opens. Click the Advanced button, then choose Add Web Reference.
  4. In the URL field, enter the following address to access the list of published SOAP web services: http://localhost:7047/DynamicsNAV90/WS/Services

    After entering the address, click the Go button located to the right of the URL field.

  5. Find the service http://localhost:7047/DynamicsNAV90/WS/Codeunit/ApplicationManagement in the list under Discovery Page, and click View Service:

    How to do it...

  6. In the Web reference name field, enter PostCodeInfoService and click Add Reference:

    How to do it...

  7. A new web reference is created in the project. Return to the code editor and include the web service namespace PostCodeInfoService in your source code. Insert the using directive inside the project namespace PostCodeWebServiceClient:
            namespace PostCodeWebServiceClient 
            { 
              using PostCodeInfoService; 
     
              class Program 
              { 
    
  8. Inside the Main function, type the code calling the web service:
            PostCodeInfo postCodeInfo = new PostCodeInfo(); 
            Console.WriteLine( 
              postCodeInfo.GetCityByPostCode("BR 22291-040")); 
            Console.ReadLine(); 
    
  9. Build and run the project. The execution result will be presented in the console window. To close the window, press Enter .

How it works...

To access the NAV web service, we start a .NET project in Visual Studio and add a reference to the service interface. Basically, this is all we need to do to obtain access to the web service. Step 2 through Step 7 describe how to add a service reference to the project. When these steps are done, Visual Studio will automatically generate the set of proxy classes wrapping all service communication details and provide access to the classes through the namespace PostCodeInfoService defined in Step 6.

The names of the classes available in the namespace match the names of web services published in NAV. The service we published is called PostCodeInfo, so this will be the name of the class inside the PostCodeInfoService namespace where the methods of codeunit 50800 Post Code Info are exposed.

The only thing left to do after creating the service reference, is to instantiate the class PostCodeInfo and call the method GetCityByPostCode. The execution result will be shown in the console window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.167.196