Consuming Web Services Using Dynamics AX MorphX

By - March 12, 2013

In this article, I will describe a tool and technique to enable Microsoft Dynamics AX 2009 (DAX) to interface with other systems; an interest of mine and I believe you will also find this article to be helpful.

I am often asked,  “how do you configure DAX to consume a webservice?”  DAX has introduced improved ways to integrate webservices from within MorphX.  So before you consider developing something outside of DAX in Visual Studio, check this out.

Simply put, a webservice is a software component that can be called using the standard internet protocols.  A webservice can be provided to the public to perform such tasks as reporting currency exchange rates, weather forecasts, shipping rates/tracking, or a web service can be developed using DAX’s Application Integration Framework for example to create a sales order, check inventory levels, report customer status, etc.

In this simple example, we are going to access an external webservice in which DAX will send in a country, state, and city; and the webservice will return the location’s latitude/longitude.

Here is how it is done:

1)  Go into the AOT and find “References”

Consuming Web Services using Dynamics AX MorphX- pic1

2)  Right click on “References” and select, “Add service reference”

Consuming Web Services using Dynamics AX MorphX- pic2

3)  Enter the WSDL URL: http://terraserver-usa.com/TerraService2.asmx?WSDL, which describes the webservice.  The .NET code namespace is the referenced namespace DAX will use in the consuming DAX class.

Consuming Web Services using Dynamics AX MorphX- pic3

4)  Press OK and Voilà!  You will find the .cs, .dll ,and the other supporting objects here:

C:Program FilesMicrosoft Dynamics AX50ApplicationAppl<DAX instanceServiceReferencesMicrosoft.Terraserver.

If you regenerate the service reference, you may have to restart the AOS.  Be sure to inform anyone involved that you are about to stop/start the Windows service.

5)  Now it is time to code the DAX class…

Create a class called “TerraService”.   The name of the class can be any valid class name.  Very important tip: the property of the class, “RunOn” must be set to run on the “server”.

Create a new method in the class, for example, “getPlaceCoordinates”.

Notice how we are referencing the “Microsoft.Terraserver” .NET namespace within the X++ code.  As you can see, the class accepts in a country, state, and city.

{
InteropPermission permission;
Microsoft.Terraserver.Place place;
Microsoft.Terraserver.PlaceFacts facts;
Microsoft.Terraserver.LonLatPt center;
Microsoft.Terraserver.TerraServiceSoapClient wsClient;
real lon,lat;
;
 
permission = new InteropPermission(InteropKind::ClrInterop);
permission.assert();
wsClient = new Microsoft.Terraserver.TerraServiceSoapClient();
 
place = new Microsoft.Terraserver.Place();
place.set_Country(country);
place.set_State(state);
place.set_City(city);
 
facts = wsClient.GetPlaceFacts(place);
center = facts.get_Center();
lon = center.get_Lon();
lat = center.get_Lat();
 
CodeAccessPermission::revertAssert();
return strfmt(“Long:%1 Lat:%2”,lon,lat);
}

 

6)  Hang on, we are almost there.  Now, let’s create a DAX job to test this class.

static void TerraService(Args _args)
{
str msg;
;
msg = TerraService::getCoordinates(“United States”,”Kansas”,”Overland Park”);
info(msg);
}

Now, execute the job.  How about that!  Overland Park, KS is on the 38th North parallel.

Enjoy!

As you can see, consuming web services from within DAX 2009 does not require a lot of time to implement and test.  I hope this article has sparked your interest in expanding Dynamics AX 2009’s capabilities to send/receive information from/to all over the World.

Please send me your questions and comments.

Thank you.

Receive Posts by Email

Subscribe and receive notifications of new posts by email.