Search code examples
debuggingdynamics-crmdynamics-crm-2015

Failing to access external SQL database from CRM Plugin while debugging


I'm having trouble accessing an external database from a CRM plugin. The error I receive is:

"Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxx' failed."

The code runs great locally within a "unit test". I made sure to set the plugin isolation mode to "none". I tried looking to this article for help, and tried everything it suggested with no luck.

Here is the current code I'm using:

var conn = new SqlConnection(@"Server=MyServer\Instance;DataBase=MyDB;User Id=MyUser;Password=MyPassword;Integrated Security=false;");
conn.Open();

I also tried this connection string and giving the NT AUTHORITY\NETWORK SERVICE user access to the database.

var conn = new SqlConnection(@"Data Source=MyDS\Instance;Initial Catalog=MyDB;Integrated Security=SSPI;");
conn.Open();

I'm on Dynamics CRM 2015 On-Premise.

Update: I found out it was working when I didn't debug, but I got the error when I try to debug it through the plugin registration tool. Any idea on why that would happen?


Solution

  • The plugin registration tool has only limited debugging capabilities. It was designed for CRM Online, where you cannot use the debugging options of Visual Studio. In OnPremise deployments use either remote debugging or install Visual Studio on the CRM Server. The last one is the recommended approach.