Consume AIF Service

Demo Overview

1.  Introduction
In Microsoft Dynamics AX 5.0, you now have the ability to consume external Web services from X++ code. After creating a reference to the Web service, you can invoke it from X++ and see the available methods with Intellisense. Calling and managing external Web services is done entirely within Microsoft Dynamics AX. This demo will show how to integrate external WCF services into Microsoft Dynamics AX 5.0 application.
2.  Audiences
The audience for this demo is developers who are responsible for creating new services for the integration of Microsoft Dynamics AX 5.0 with other systems.
3.  Scenario
·  Personas
·  Tim Jones – Director of Sales
·  Kate Light – Developer
·  Story
As part of the Sales Department’s activities, tracking the company’s quotes on the market is particularly important. Tim Jones, Director of Sales, addressed the company developer, Kate Light, for a solution that would implement a service with this functionality and integrate it into Microsoft Dynamics AX. Kate decided to integrate the new service and use it in further implementation by means of AIF tools. She has already implemented the TradeService WCF service and published it on IIS. Kate will configure the service and consume it inside X++ code.
4.  Purpose of Demo
The demo shows:
·  How to get access to service definition (WSDL)
·  How to create a service reference for external Web service using Microsoft Dynamics AX development tools
·  How to generate, deploy, and configure all artefacts necessary to integrate an external Web service with Microsoft Dynamics AX application
·  How to call the Web service from X++
5.  Setup/ Prerequisites
This demo requires the following preliminary settings:
·  Access to the WSDL of the external TradeService Web service
·  .NET Framework 3.0 SDK must be installed
·  The remote Web service must comply with the following requirements:
·  Support all .NET data types of parameters and return values of Web methods
·  Comply with the following standards: [WSBP1.1], [SOAP1.1] and [SOAP1.2]
·  Publish service definitions in [WSDL1.1] or in [WSDL2.0]
·  The Web reference’s WSDL must be available in the specified location (UNC path or URL)

Demo

Presenter’s Discussion Points / Actions Taken
Kate has already implemented a new service named TradeService and published it under the WCFServices virtual directory on IIS. The Bin directory contains QuickReturns.StockTrading.ExchangeService.dll.
Kate browses for the ExchangeService.svc file, which contains the WSDL service definition of the TradeService, to verify that the service was successfully published on IIS. / Open Internet Information Services (IIS) Manager from Administrative tools. Click Local computer > Web Sites > MicrosoftDynamicsAXAif50 > ExchangeService.
Right-click the ExchangeService.svc file and select Browse.


First of all, Kate creates a new Service reference that can be used to invoke TradeService from the X++ code. / Click Start All Programs Microsoft Dynamics AX.
Click AOT\References. Right-click the References node and select Add service reference.

In the WSDL URL field, she enters the location of the service WSDL. This field can contain a path to the local file system, a file share, or an Internet address.
Next, she fills in the .NET code namespace field. This name is used as the name of the .NET assembly that is created and the name of the directory that contains the generated Web reference files. / In the Add service reference dialogue enter the following information:
WSDL URL:
http://dynamicsvm.contoso.com/MicrosoftDynamicsAXAif50/ExchangeService/ExchangeService.svc?wsdl
.NET code namespace: QuickReturns.StockTrading.Service
Reference name: QuickReturns.StockTrading.Service
Click OK.

When adding a new service reference to Microsoft Dynamics AX, the WSDL file of the external Web service is retrieved. Based on this file, the following .NET artifacts are generated for the service reference:
·  .NET service proxy
·  WCF client configuration file (app.config)
A Web reference for the Web service has been created and deployed to the network share under the application tree.
Now the creation of the Web reference is complete. / As a result, the following Infolog appers:

Under the network share (C:\Program Files\Microsoft Dynamics AX\50\Application\Appl\DynamicsAx1\ServiceReferences\QuickReturns.StockTrading.Service), a new folder for QuickReturns.StockTrading.Service was created and the generated artifacts were stored there:

A new service reference appears in AOT. / AOT\References

Kate wants to verify the configuration of the service. / Open Service references from Basic Setup Application Integration Framework.
Select QuickReturns.StockTrading.Service and click Configure service.

SvcConfigEditor is launched in a separate window. It automatically loads the configuration file for the selected Web reference. The configuration details can be viewed and changed in this file.
The configuration changes are saved in the Web reference’s configuration on the network share. They are applied immediately; a re-compilation, re-deployment, or restart is not required. / Go through the Configuration nodes in SvcConfigEditor.

Open Endpoints in the Client node.

Open Bindings in the Client node.

Close SvcConfigEditor.
Kate creates the new TradingService class to consume the service. / Open AOT\Classes
Create a new class with the name TradingService.
Open the class properties and set the RunOn property to Server.

She adds the new getQuote method that will invoke the service methods. / Add a new method with the following code to the TradingService class:
public void getQuote()
{
InteropPermission interopPermission =
new InteropPermission(InteropKind::ClrInterop);
System.Exception clrException;
QuickReturns.StockTrading.Service.Client service;
QuickReturns.StockTrading.Service.Quote quote, resultQuote;
System.Decimal bid, bidPublished, bidResult;
str ticker, bidStr;
;
interopPermission.assert();
try
{
service = new QuickReturns.StockTrading.Service.Client();
quote = new QuickReturns.StockTrading.Service.Quote();
resultQuote = new QuickReturns.StockTrading.Service.Quote();
quote.set_Ticker("LoghtCompany");
bid = new System.Decimal(30.5);
quote.set_Bid(bid);
service.PublishQuote(quote);
resultQuote = service.GetQuote(quote.get_Ticker());
bidResult = resultQuote.get_Bid();
bidStr = bidResult.ToString();
ticker = resultQuote.get_Ticker();
info(strFmt("Quote publishing results: Ticker- %1, Bid - %2",ticker,bidStr));
}
catch
{
// Get the last CLR exception object
clrException = ClrInterop::getLastException();
// Walk through CLR exceptions.
while (clrException)
{
// Announce the error
error(clrException.get_Message());
// Go to the inner exception
clrException = clrException.get_InnerException();
}
// Thow an Axapta error exception
throw Exception::Error;
}
}
Next, Kate adds the main method to the class. / Add a new method with the following code:
public static void main(Args args)
{
TradingService TradingService = new TradingService();
;
TradingService.getQuote();
}
And then runs the class. / Press F5.
Kate sees that service operations are called correctly. /

11