#1. Develop and Debug in Sandbox Isolation Mode (its faster)
If you ever want your plug-ins to work on CRM Online then you need to make sure you're plug-ins work in sandbox isolation mode. And you will be surprised to learn what works and doesnt work when in sandbox mode. These are just a few of the things I've run into that don't work in sandboxed plug-ins
- Calling a web-service using IP address or localhost.
- Creating the WCF service proxy class by calling its constructor.
- CRM LINQ queries
- Calling a web service using default credentials
Also, it is now actually easier to debug plug-ins when they are sand-boxed. When registering a plug-in, you have three options of where the plug-in can be stored.
- On Disk - The plug-in assembly is stored on the CRM server file system (in the Server\Bin\Assembly folder).
- Database - The plug-in assembly is actually uploaded to the CRM server and stored in the CRM database.
- GAC - The plug-in is stored in the global assembly cache on the CRM server.
In previous versions of CRM if you wanted to debug a plug-in your only option was to register the plug-in on DISK. This meant that everytime you made a change to your plug-in code you had to re-copy the file to the assembly folder (which also meant you had to restart IIS and the CRM Asynchronous service).
Now you can debug the plug-in even if it's stored in the database and in sandbox mode, and it's actually a much faster development cycle because you can avoid restarting IIS and the Asyc service.
Debugging a plug-in is simple, just follow the instructions in the SDK found here. Essentially, there are three differences from debuggin an on-disk plug-in
- Add the SandboxDebugPlugins key to the registery (see the instructions in the SDK).
- Copy the PDB file for your plug-in assembly to the server\bin\assembly folder.
- In Visual Studio, attach the debugger to all of the "Microsoft.Crm.Sandbox.WorkerProcess.exe" processes.
In fact, you can make the process of re-uploading the plug-in DLL to CRM very fast with a simple console application. All the console app needs to do is connect to the CRM server, and update the "Content" attribute of the PluginAssembly record for your plug-in. The following code should do the trick:
QueryExpression query = new QueryExpression(PluginAssembly.EntityLogicalName);
query.ColumnSet = new ColumnSet("name");
query.Criteria.Conditions.Add(new ConditionExpression("name", ConditionOperator.Equal, myAssemblyName));
EntityCollection retrievedAssemblies = _serviceProxy.RetrieveMultiple(query);
if (retrievedAssemblies.Entities.Count > 0)
{
PluginAssembly myAssembly = (PluginAssembly)retrievedAssemblies.Entities[0];
byte[] bytes = File.ReadAllBytes(myAssemblyPath);
myAssembly.Content = Convert.ToBase64String(bytes);
_serviceProxy.Update(myAssembly);
}
If you also automate the copying of the PDB file into the server\bin\assembly folder then you can actually get at pretty quick debugging cycle going.