I've now moved to an environment that uses Dynamics Ax 2012 R3. Today I was asked how to reference a .Net Assembly from Ax code. It took a little trial and error, but it was very easy.
First I created a C# Class Library project in Visual Studio. The project contained one class and 2 method.
using System;
namespace AxTestClassProject
{
public class AxTestClass
{
public static string HelloWorld()
{
return "Hello World";
}
public string TestInstanceMethod()
{
return "Test Successful";
}
}
}
I then compiled the project and added it to the AOT. Next, I had to manually copy and paste the DLL from the bin\debug directory to the bin directory of the client.
Next, inside the AOT, I added a reference to the new DLL.
Finally, I created a job that referenced the 2 methods. I experimented with code access permissions, but found they were not needed. I only commented out, because it is needed if dynamic typing is used.
static void TestAxTestClassProject(Args _args)
{
//InteropPermission perm;
System.String clrStr;
AxTestClassProject.AxTestClass testClass = new AxTestClassProject.AxTestClass();
str axStr;
/*perm = new InteropPermission(InteropKind::ClrInterop);
if (perm == null)
{
return;
}
perm.assert();*/
clrStr = AxTestClassProject.AxTestClass::HelloWorld();
axStr = clrStr;
info(axStr);
clrStr = testClass.TestInstanceMethod();
axStr = clrStr;
info(axStr);
//CodeAccessPermission::revertAssert();
}
Running the job produced the expected results.
Comments
- Notice that all references to .Net classes must be fully qualified. There are no using or imports statements in 2012 R3.
- I decided on being cautious by direct marshalling .Net strings to x++ strings. In this case, it does not appear to be needed. However, if any manipulations at required between .Net primitive types and x++ primitive types, these direct assignments are required.
- For 2012 R3, the .Net project must be linked to (target framework set to) .Net 4.0 (i.e. not the client framework).
- You should be able to reference any FCL classes or other assemblies in the GAC without additional references.
No comments:
Post a Comment