Monday, June 1, 2015

Microsoft Dynamics AX 2012 Class Libraries (DLLs)

Microsoft Dynamics AX 2012 Class Libraries (DLLs)
 
Purpose: The purpose of this document is to illustrate how to work with class libraries in the context of Microsoft Dynamics AX 2012.
 
Challenge: For the purposes of POC, integration project or development project you may need to consume Microsoft Dynamics AX 2012 Web Services from external class libraries or call external Web Services from Microsoft Dynamics AX 2012 managed code assemblies.
 
Solution: In this walkthrough I'm going to highlight number of ways you can consume Microsoft Dynamics AX 2012 Web Services from external class library using configuration settings in application config file or handled in the application code. Also I will highlight how develop Microsoft Dynamics AX 2012 managed code assemblies and then call external Web Services from there.
 
Walkthrough
 
Calling Microsoft Dynamics AX 2012 Web Service from external Class Library (DLL)
 
First of all I will expose Microsoft Dynamics AX 2012 Web Services via Inbound ports using NET.TCP and HTTP adapters
 
Inbound port (NET.TCP)
 
 
Inbound port (HTTP)
 
 
IIS
 
 
After Web Services have been exposed through Inbound ports in your client application you can Add Service Reference using WSDL URI as shown below
 
Add Service Reference (NET.TCP)
 
 
Add Service Reference (HTTP)
 
 
Added Service Reference will show up in your project as below
 
Solution Explorer (Console App)
 
 
Please note that corresponding Endpoint and Binding details will be added to App.config after you added Service Reference
 
App.config
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_AdvancedLedgerEntryService" />
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://ax2012r2a:8201/DynamicsAx/Services/AlexServices"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_AdvancedLedgerEntryService"
contract="ServiceReference1.AdvancedLedgerEntryService" name="NetTcpBinding_AdvancedLedgerEntryService">
                <identity>
                    <userPrincipalName value="admin@Contoso.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>
 
Now I’ll create a class library project and Add Service Reference too
 
Solution Explorer (Class Library)
 
 
Similarly to above corresponding Endpoint and Binding details will be added to App.config 
 
App.config
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_AdvancedLedgerEntryService" />
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://ax2012r2a:8201/DynamicsAx/Services/AlexServices"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_AdvancedLedgerEntryService"
contract="ServiceReference1.AdvancedLedgerEntryService" name="NetTcpBinding_AdvancedLedgerEntryService">
                <identity>
                    <userPrincipalName value="admin@Contoso.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>
 
In fact when leveraging business logic from class library you have to use client application config file instead of original DLL config file. That’s why I will copy Endpoint and Binding details from DLL config file over to client app config file
 
Client application Config file
 
<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_AlexService" />
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://ax2012r2a:8201/DynamicsAx/Services/AlexServices"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_AlexService"
                contract="AXServiceReference.AlexService" name="NetTcpBinding_AlexService">
                <identity>
                    <userPrincipalName value="admin@Contoso.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>
 
Now my client application will be well aware about Endpoint and Binding details for Microsoft Dynamics AX 2012 Web Service
 
Alternatively you can handle Endpoint and Binding details for Microsoft Dynamics AX 2012 Web Service directly in client application code as shown below  
 
Client application Code
 
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
 
EndpointAddress address = new EndpointAddress("http://ax2012r2a.contoso.com/MicrosoftDynamicsAXAif60/AlexWebServices/xppservice.svc");
 
AlexServiceClient client = new AlexServiceClient(binding, address);
 
CallContext context = new CallContext();
context.Company = "usmf";
 
client.ClientCredentials.Windows.ClientCredential.Domain = "contoso";
client.ClientCredentials.Windows.ClientCredential.UserName = "Admin";
client.ClientCredentials.Windows.ClientCredential.Password = "pass@word1";
 
try
{
    client.method1(context);
}
catch (Exception e)
{
 
}
 
Call external Web Service from Microsoft Dynamics AX 2012 Class Library (DLL)
 
In the second part of the walkthrough I’m going to wrap a business logic calling external Web Service with Microsoft Dynamics AX 2012 managed code assembly
 
First off I’ll create Class Library project with minimalistic implementation for a single class and method which mimic calling external Web Service
 
Class Library  
 
 
As coding is complete I’ll add Class Library project directly into Microsoft Dynamics AX 2012 AOT. Please note that I have Microsoft Dynamics AX 2012 Visual Studio Tools installed to accomplish this: https://technet.microsoft.com/en-us/library/dd309576.aspx
 
My project now will change its icon to Microsoft Dynamics specific one, after that I’ll be able to select auto-deployment options in Project Properties. Specifically I will choose to deploy assembly to Microsoft Dynamics AX 2012 Client (Yes) and Server (Yes)
 
Class Library (Deployment)
 
 
After successful deployment my project will show up in Microsoft Dynamics AX 2012 AOT
 
AOT – C Sharp Projects
 
 
Here’s the implementation of class and method which mimic calling external Web Service from within Microsoft Dynamics AX 2012 managed code assembly
 
Source code
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AlexDLL
{
    public class Class1
    {
        public string method1()
        {
            return "External Web Service call";
        }
    }
}
 
Generally it would be a good idea to encapsulate business logic calling external Web Service in a single method in managed code assembly, this way you can simply call a single method in X++ to consume Web Service and obtain the result
 
When deploying assemblies from within Visual Studio you may need to make sure that “Enable the hot-swapping of assemblies for each development session” setting is selected in Microsoft Dynamics AX 2012 Server Configuration Utility in order to avoid deployment errors
 
Microsoft Dynamics AX 2012 Server Configuration Utility
 
 
Before you deploy you will need to Sign the assembly in Project Properties > Signing
 
Create Strong Name Key
 
 
Now we are ready to deploy the assembly
 
Deploy
 
 
Upon successful deployment you will be able to use IntelliSense in X++ and consume elements of managed code assembly namespace just the way you consume native X++ business logic classes
 
X++ code
 
static void DLL(Args _args)
{
    AlexDLL.Class1 class1 = new AlexDLL.Class1();
 
    info(class1.method1());
}
 
The result will look like below
 
Result
 
 
Please learn more about Deploying Managed Code Assemblies here: https://msdn.microsoft.com/en-us/library/gg889192.aspx
 
Summary: In this walkthrough I illustrated how to call Microsoft Dynamics AX 2012 SOAP Web Services from Windows 8 application using JavaScript and shed some light into what's happening behind the scenes when you call Web Service, how request and response look like and what you have to do to successfully call Microsoft Dynamics AX 2012 SOAP-based Web Services.
 
Tags: Microsoft Dynamics AX 2012, Custom Web Services, X++, C#.NET, HTTP, NET.TCP, Config file, Class library, DLL, Endpoint, Binding.
 
Note: This document is intended for information purposes only, presented as it is with no warranties from the author. This document may be updated with more content to better outline the issues and describe the solutions.
 
Author: Alex Anikiev, PhD, MCP

19 comments:

  1. Recipe costing software Our recipe, menu management and food costing software frees up your time so you can focus on running your business.

    ReplyDelete
  2. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    J2ee training in chennai

    ReplyDelete
  3. Dear Alex,

    We are trying to integrate 2 separate AX implementations using AIF.

    Steps:
    1. Create AIF service in AX environment 1.
    2. Wrote a .NET wrapper around the AIF, using a class library project type and added the service reference.
    3. Deployed class library project in AOT of AX environment 2 and also added classlibrary.dll and classlibrary.dll.config in the Bin folder of 2nd environment and than added reference to dll in the AOT.
    4. We finally wrote X++ job to call(consume) the class library that was added in the AOT.

    Exception :
    System.InvalidOperationException: Could not find default endpoint element that references contract 'AMSSVCSR.HREMPAIFService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.



    ReplyDelete

  4. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.

    Online Reputation Management

    ReplyDelete
  5. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

    dot net training in chennai

    php training in chennai

    java training in chennai

    ReplyDelete
  6. for preparing bank exam and group exam , we are providing an online test model questions papers

    Bank Exam Questions and Answers

    Group Exam Questions and Answers

    ReplyDelete
  7. Great post going through your articles..this is really helping.

    QTP Training in Chennai

    ReplyDelete
  8. Hi there, You have done an incredible job. I’ll definitely digg it and personally recommend to my friends. I’m sure they’ll be benefited from this site..Keep update more excellent posts..

    Back to original

    ReplyDelete
  9. This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.


    Best Dental Clinic In Vellore

    ReplyDelete


  10. Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this content.

    Digital Marketing Company in Chennai

    ReplyDelete
  11. This blog is very well done, thus it is very well useful too, so i am expecting more things from your blog.

    Digital Marketing Company in Chennai

    ReplyDelete
  12. I read your articles very excellent and the i agree our all points because all is very good information provided this through in the post.

    Web Design Company in Chennai

    ReplyDelete
  13. I read your articles very excellent and the i agree our all points because all is very good information provided this through in the post.

    Digital Marketing Company in Chennai

    ReplyDelete
  14. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.

    AWS Training in Chennai

    ReplyDelete
  15. I was researching on the best methods of How to Compose a College Research Paper before I landed on this page and I have found an interesting blog with great article and design that is worth commenting on. I am looking forward to reading more interesting and intriguing articles from this site and I hope the writer will continually keep us updated with new articles.

    ReplyDelete

  16. Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

    Installment loans
    Payday loans
    Title loans

    ReplyDelete