Dynamics 365 for Operations (a.k.a. AX7) provides several endpoints for web service. In this blog post, I want to describe consuming a D365O custom web service in a C# application using the SOAP endpoint.
For a detailed description about service endpoints, you can read the official documentation at https://docs.microsoft.com/en-us/dynamics365/operations/dev-itpro/data-entities/services-home-page.
The main advantage of the SOAP protocol is its descriptive functionality through the WSDL language. SOAP endpoints provide detailed description about contracts and parameters to call each service method. Visual Studio has a great functionality that can read the service description and automatically generate proxy classes to access the service methods.
Let’s do an example of consuming a D365O web service in Visual Studio.
- Run Visual Studio and go to File – New Project, go to Templates – Visual C# – Windows Classic Desktop on the left side of the window and select Console App on the right side. Enter the name of the project and click OK.
Let’s add a new service reference and generate proxy classes.
- Right click on Visual Studio project and select Add – Service reference
In opened window, we need to enter URL to WSDL of our service. In this example, I propose to consume the D365O standard financial service so the WSDL URL would be something like…
https://{AOSANAME}.cloudax.dynamics.com/soap/services/financialdimensionservice
- Enter FinancialServices in the Namespace field. Click OK and Visual Studio will generate proxy classes for the financial services endpoint.
You can see the generated classes in Object browser (right click on new node FinancialServices under the Connected services node).
On this screenshot, you can see automatically generated classes (contracts and client).
To make web authentification you will need to add some libraries.
- Right click on Visual Studio project and select Manage NuGet Packages.
- Type IdentityModel in the search box for searching packages.
- Visual Studio will find the Microsoft library Microsoft.IdentityModel.Clients.ActiveDirectory. Select this library and click Install button on right side of this window. And click Ok to continue.
Let’s try to consume service.
Before consuming the financial service, we need to complete authentication. We will authenticate through web authentication.
Declare some configuration variables:
…and here’s the authentication logic:
Now get client binding
We want to get a list of financial attributes and them values. Create the financial service request.
The CallContext class is used to the pass the company, language, user id and partitional key to the service.
Source code for the method GetBinding
Source code for method GetSoapServiceUriString
Full source code: D365O_SOAPEndpointExample
NOTE: Once you download this source code, you must change placeholders {AOSNAME} and {COMPANY_DOMAIN} in the source code.
The example of changing placeholders:
NOTE: Once you download this source code, you must change placeholders {AOSNAME} and {COMPANY_DOMAIN} in the source code.
The example of changing placeholders:
{AOSNAME} = usnconeboxax1aos
{COMPANY_DOMAIN} = contoso.com
{COMPANY_DOMAIN} = contoso.com
Also, you will need to configure Azure Active Directory applications. Go in D365 menu System Administration -> Setup -> Azure Active Directory applications.
How to do it, you can read in blog https://community.dynamics.com/ax/b/axforretail/archive/2016/09/26/service-to-service-authentication-in-ax7
How to do it, you can read in blog https://community.dynamics.com/ax/b/axforretail/archive/2016/09/26/service-to-service-authentication-in-ax7
Let’s build and run the project.
When you run this project you will see Dimensions and their values.
Comments
Post a Comment