.NET Integration

Introduction
There are 2 different types of .NET Integration. First you may use .NET Assemblies inside Dynamics AX via CLR Interop. Second you can use X++ Code from outside by the .NET Business Connector. By Default you can use the System Namespace in your Application.

System.Collection.SortedList list = new System.Collection.SortedList;

CLR Interop
If you're using Visual Studio create a new Class Library Project. Implement whatever you need. In the Project Properties you have to sign your Assembly. Only signed Assemblies can be used in Dynamics AX. Change the Build Type to Release and build your Project. You find your DLL in your Projects bin/Release folder. Copy the DLL to the Dynamics AX Client/Bin Directory or register the Assembly in the Global Assembly Cache. Use the gacutil tool to register your Assembly in the GAC.

gacutil /i yourlibrary.dll

Open the AOT in your Dynamics AX Client. Add your Assembly to the References Node. If you have registered the Assembly in the GAC you can choose it from the grid. Otherwise you have to provide the path. If you don't get any errors it should work. It is a good idea to use Code Access Permissions with CLR Interop. Otherwise you will get a Best Practice Warning

CodeAccessPermission permission; MyLibrary.MyClass foo;

permission = new InteropPermission(InteropKind::CLRInterop); permission.assert;

foo = new MyLibrary.MyClass;

Restrictions


 * CLR Interop in Dynamics AX does not support event handling. Events are only supported by ActiveX controls in forms. Although the wrapper methods are generated, its not possible to assing a delegate. A possible solution might be to write a .NET Wrapper object that handles and enqueues the events. In AX you would have to write a thread that checks every n miliseconds if there are new events in the queue. But thats not very performant.
 * Output parameters are not supported. It would be necessary to use the keyword "out" wich is not part of X++. Because output parameters are bad programming style in general write a .NET wrapper that returns the output value or throws an exception if necessary.

.NET Business Connector
The Business Connector is inverse to CRL Interop. It allows you to use Dynamcis AX Objects outside Dynamics AX. Typically scenarios are Load Testing using the Load Test Tool and Webservices. First you need to configure the Business Connector with the Client Configuration Tool. Next you have to provide a Proxy User. Create a new User Account in Active Directory. The Proxy User should be in the Administrator Group. In Dynamics AX you have to configure the Proxy User under Configuration > Security > BC Proxy User. Provide Username and Domain.

Time to test the Business Connector; Start Visual Studio and create a new Console Application. Add the Reference to the Microsoft Business Connector .NET DLL. You find the DLL in the Client/Bin Directory. Try to connect with Dynamcis:

try { Axapta axapta = new Axapta; axapta.Login("","","",""); Console.WriteLine("It works"); axapta.Logoff; } catch(Exception ex) { Console.WriteLine(ex.ToString); } Console.ReadKey;

If you are using the Business Connector in a Web Application you have to use the LogonAs Method try {

System.Net.NetworkCredential credential = new System.Net.NetworkCredential("BCProxyUser","pass123","mydomain"); Axapta axapta = new Axapta axapta.LogonAs("BCProxyUser","mydomain",credential,"","","",""); .. axapta.Logoff; }

Microsoft.Dynamics.BusinessConnectorNet Namespace
Most classes in the business connector namespace are exception classes. There are 5 worker classes to be used for development with .NET.


 * Axapta: This class is the first to be touched. There are serval methods implemented to logon in Dynamics Ax. Beside the Logon and Logoff the Axapta class is necessary to create any type of AxaptaObjects. Typically classes implemented in Dynamics AX with X++. You may call Jobs and static methods on a class.
 * AxaptaObject: An Axapta Object is typically a class. There are 5 ways to call a method on a class. In every way you have to provide the method name. The simplest way is to call a method without parameters. Next the Call method is overloaded with 1 .. 3 parameter objects. And finally a Call with an array of parameters for methods with more then 3 parameters.
 * AxaptaRecord: As the name suggests this .Net class represents a record in Dynamics AX. It has all the methods you know from X++ development.
 * AxaptaContainer.
 * AxatapBuffer



Exception Classes

Enumerations
Unfortunately there is no support for Enumerations in Dynamics AX 4. One way to get around this limitation is to write a factory method in C# that creates Enum values by an index (or name if preferred). This version is hardcoded, there may be a better way to create all parameters and call all methods using Reflection.

namespace Demo { public enum MyEnum {   One,Two,Three }

public class AxEnumFactory {   // Factory method public static MyEnum CreateMyEnumByIndex(int idx) {     switch(idx) {       case 1: return MyEnum.One; case 2: return MyEnum.Two; case 3: return MyEnum.Three; default: return MyEnum.One; // At least something valid }   }  }  public class MyBusinessLogic {   public void Foo(MyEnum parm) {     // Business logic here ...     } }

}

In Dynamics AX 4 use the factory class to initialize and set enumeration parameters and variables. Demo.MyBusinessLogic worker = new Demo.MyBusinessLogic; worker.Foo(Demo.AxEnumFactory::CreateMyEnumByIndex(2));

Misc. Notes
Sometimes when working with .net code that calls customer X++ code you will need

to re-index the AOT. This insures that code changes in X++ are being used when you

run the .net code accessing it.

Here is a batch script to re-index your AOS.