Accessing logic in an external web service

Now that we have seen how we can make other applications trigger the AX logic by exposing the AX logic to web services, we will turn the other way around and see how we can make AX use web services that can expose logic from external applications.

Accessing logic in an external web service

In the preceding diagram, we see that AX uses a web service proxy that is created specifically for the web service that it is about to call. The web service proxy lets AX know everything it needs to know in order to call the web service through the Internet. The web service, in turn, will return a result from the external application and pass it back to AX.

A lot of the examples on the web will show you how you can ask a web service to convert from one currency to another, checking if an email address is valid and some others that most likely is less useful to your AX customers.

Sometimes, however, you will find that your AX customers have several other applications that AX should integrate with to get additional information. Maybe they could even integrate with their vendors to get updated product information automatically from their vendors' applications. If these applications can have web services that trigger actions within these applications, you can consume these web services in AX so that AX can ask for data or trigger some event in the other application.

To show how to achieve this, we will continue to use the example shown earlier. Our web service will then return a result from AX, but it could have been a result returned from any other application that supports web services as well.

Creating a service reference

The first thing we need to do is add a service reference to our web service. We perform this action in Visual Studio, and then add the new project to the AOT. The steps are as follows:

  1. To do this, simply open the Visual Studio and create new C# class library project. Call the project WS.ItemOnHand.
  2. Under Solution Explorer, right-click on the project and select Add Service Reference.
  3. Type in the WSDL and change namespace to OnHand.
  4. Right-click on the project and select Add to AOT.
  5. Make changes in the project properties so both Deploy to client & Deploy to Server options are set to Yes.
    Creating a service reference

The WDSL URL is the location where the service description of the web service resides.

The .NET code namespace is the namespace that the proxy class will have. The proxy .dll file will also be put in a directory corresponding with .NET namespace.

The reference name is the name of the reference to the proxy that we will use in AX. It will typically be the same as .NET code namespace to have the same naming in the proxy class and in AX.

The service description is just a simple description of the service so that other people will easily recognize what this service is used for in AX.

After filling out the required information in the box, click on OK. After a little while, you should see an infolog with information regarding the creation of the proxy class. There should be no errors or warnings in the infolog.

Creating a class that consumes the web service

We can now create a class that uses the web service:

  1. Create a new class and call it OnHand. Add a new method that looks like this:
    public static server str getOnHand(str itemId),
    {
      // Reference the class in the WS.ItemOnHand namespace
      // Remember that the namespace and its classes are case sensitive
      WS.ItemOnHand.OnHand.ItemOnHandSoapClient soapClient;
      str ret;
      try
      {
        // Make sure that we get permission to use the clr interop
        new InteropPermission(InteropKind::ClrInterop).assert();
        // Create a new object of the ItemOnHandSoapClient class
        // The endpoint configuration must be equal to the
        // wsdl:portType name value in the WSDL file
        soapClient = new WS.ItemOnHand.ItemOnHandSoapClient('ItemOnHandSoap'),
        // Execute one of the method in the web service
        ret = soapClient.AvailableNow(itemId);
        // Revert the clr interop access when we are done
        // using it.
        CodeAccessPermission::revertAssert();
      }
      catch (Exception::CLRError)
      {
        throw error(AifUtil::getClrErrorMessage());
      }
      return ret;
    }

    Note

    A method that executes an object that requires ClrInteropPermission must be executed on the server by a static method.

  2. We also will create a main method as a starting point of our class:
    static void main(Args args)
    {
      str itemId;
      itemId = "1161";
      info (strfmt("On-hand for item %1: %2", itemId, OnHand::getOnHand(itemId)));
    }

If we execute the previous class now, we get the same result as we did when we were testing the web service directly from the web browser earlier in this chapter:

Creating a class that consumes the web service
..................Content has been hidden....................

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