Search code examples
azurepluginsdynamics-crmmicrosoft-dynamics

Dynamics CRM 365 (Online) plugin exception EnvironmentPermission using Azure Application Insights


I am trying to track custom events using a plugin and Azure Application Insights. When I build the plugin, I use MSBuild.ILMerge.Task to merge the Application.Insights.dll with my plugin assembly. Here are the steps I followed to do the merge: http://dynamicscrmcoe.com/il-merge-dynamics-crm-plugins/

The problem I have is that when I try to instantiate the Telemetry client:

var telemetryClient = new TelemetryClient();

I get this error:

Request for the permission of type System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I have tried granting permissions by using security attributes:

[EnvironmentPermission(SecurityAction.Assert, Unrestricted = true)]

and have also tried

[PermissionSet(SecurityAction.Assert, Name = "FullTrust")]

But nothing seems to work. Any ideas/advice would be appreciated. Thanks in advance.


Solution

  • The short answer is you cannot do this. The current release of Application Insights does not support the code restrictions placed on CRM sandboxed plugins.

    This has been reported and resolved as an issue: https://github.com/Microsoft/ApplicationInsights-dotnet/issues/416.

    I pulled down this code and it resolves the problem it was attempting to solve, i.e. certain machine information calls. It still throws an error because it expects to be able to attempt to read the file system - which it can't.

    Application Insights is designed to be implemented at at the base level of an application (usually, but not always, a web application.) The code you are writing, when creating custom plugins and workflow activities, is not that code. It is a plugin to a base level application and that base level application does not implement Application Insights.

    That said, there is not much need for Application Insights in a sandbox plugin. Dynamics CRM already tracks plugin execution statistics for sandbox plugin which can be viewed via Advanced Find.

    Advanced Find Search for Plugin Statistics

    For detailed tracing, especially for exception tracing, you have the ITracingService within the plugin.