CLRInterop in x++ and managing exceptions or finding CLR errors[Dynamics AX ]

Friends, we can access the Classes in assemblies that are managed by the common language runtime (CLR) in X++ code.

This feature of Microsoft Dynamics AX is called common language runtime interoperability, or CLR interop.

The CLR interop feature works only in the direction from X++ calling into CLR managed assemblies. It does not support calling X++ classes from a CLR managed assembly.

CLR interop is useful when you want your X++ code to access the functionalities in a CLR managed assembly. This includes assemblies that are installed with the .NET Framework, and any assemblies that you create with a language such as C# or Visual Basic.NET

Below example is to validate an email address using regular expressions but by invoking the classes in .NET Framework.

Note: I have received comments on why not use AifUtil::getclrerrormessage() instead of reinventing the wheel..well simple answer would be not everyone will have access to AIF and the below code can handle different exceptions.

static void SR_ValidateEmail(Args _args)
{
str email = ‘sr@yahoo.com’;
str emailAddressFilter = @”^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$”;
System.Text.RegularExpressions.Regex regEx;
System.Text.RegularExpressions.Match regMatch;
InteropPermission permission = new InteropPermission(InteropKind::ClrInterop);
boolean retVal;
permission.assert();
//BP Deviation documented
regEx = new System.Text.RegularExpressions.Regex(emailAddressFilter);
regMatch = regEx.Match(email);
retVal = regMatch.get_Success();

if(retval)
{
info(“valid email”);
}
else
{
throw error(“invalid email address”);
}
}

However during invocation of .net classes there could be exceptions that have to be taken care of or handled.[Could be errors or Internal exceptions]

Below code will help you to capture the last CLRExceptions. Always place the business logic in try and catch blocks

static void validateEmail(Args _args)
{
str strError;
CLRObject exc;
CLRObject clrExcMessage;
CLRObject innerExc;

try
{
// Code goes here to validate email
}
catch( Exception::Internal )
{

// BP Deviation Documented
exc = CLRInterop::getLastException();
if( exc )
{
clrExcMessage = exc.get_Message();
// BP Deviation Documented
strError = CLRInterop::getAnyTypeForObject( clrExcMessage );

throw error(strError);
}
}
catch( Exception::CLRError )
{

// BP Deviation Documented
exc = CLRInterop::getLastException();
if( exc )
{
clrExcMessage = exc.get_Message();
// BP Deviation Documented
strError = CLRInterop::getAnyTypeForObject( clrExcMessage );

innerExc = exc.get_InnerException();
while(innerExc != null)
{
clrExcMessage = innerExc.get_Message();
// BP Deviation Documented
strError = strError + ‘\n’ + CLRInterop::getAnyTypeForObject( clrExcMessage );
innerExc = innerExc.get_InnerException();
}

throw error(strError);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: