I am working with Microsoft Dynamics AX 2009 and when calling a method on one of my C# assemblies from X++ I got the error:

Request for the permission of type 'InteropPermission' failed.

image

After reading a few misguided newsgroup postings I found this article showing how to get around the error. Apparently in Axapta 2009 the hosted CLR requires that you assert permissions before you make a call to your assembly.

http://msdn.microsoft.com/en-us/library/bb190039.aspx

So I modified my code to include the InteropPermission class, instantiated it as a new object and called assert. Well I immediately noticed that I needed to construct the InteropPermission class with an InteropKind enumerator. So after a quick search I found this article:

http://msdn.microsoft.com/en-us/library/aa605505.aspx

Long story short, you can call your CLR code by using code like this:

   1:  void sendSalesOrderConfirmNotification(str salesOrderNumber)
   2:  {
   3:      XppNotifier.Notifier notifier;
   4:      XppNotifier.NotificationType notificationType;
   5:      InteropConfigTable iConfig;
   6:      KeyDataType key;
   7:      InteropPermission perm;
   8:    ;
   9:   
  10:      key = "NotificationMSMQQueuePath";
  11:      perm = new InteropPermission( InteropKind::ClrInterop );
  12:      perm.assert();
  13:   
  14:      notifier = new XppNotifier.Notifier();
  15:      notifier.FireNotification(iConfig.getValue(key), salesOrderNumber,  "", "", XppNotifier.NotificationType::SalesOrderConfirmation);
  16:   
  17:    info("Sales order confirmed, sending notifications: SalesId: " + salesOrderNumber);
  18:   
  19:   
  20:  }

Note: My C# assembly is the XppNotifier. I have to create an InteropPermission variable at line 7. Then at line 11 I instantiate the InteropPermission class. I pass in the ClrInterop enum because I am requesting access to call my C# assembly. On line 12 I make the assertion. This basically says to the CLR “Hey, I want to run some managed code. Is this cool?” the CLR then either throws a Code Access Security exception (CAS Exception) or returns quietly. However now, the current application domain that is executing has the right to invoke the C# assembly.

 

 

第二篇:

Error: "Request for the permission of type 'InteropPermission' failed."

 
Hi there,

Here's another error message you may come across in Dynamics Ax:

Error: "Request for the permission of type 'InteropPermission' failed."

You have written some code, tested it and everything went fine. Now the project has gone live, and all of a sudden, the code fails with the error message from above.
The code is probably executed in another context as when you tested it. For example by another user, or in batch.
The reason for the failure is the security measures that were implemented in Ax 4.0 and up.
What do you need to do? Assign permissions to execute the code (CLR Interop). Like this:

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


If your error message is regarding a COM object that you are referencing, you can use this alternative:

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

You can read more about CAS or Code Access Security over at MSDN.