Another Options for Referencing External Assemblies from Plugins

As programmers, we are always taught to reuse code.  Follow the DRY principal they say (Don't Repeat Yourself).  In .Net development code re-use is a snap; That is, unless you are coding in CRM plugins or custom workflow assemblies.  Your code may needs to be available for use in the offline client so everything the offline client uses must be installed into the database.  Unfortunately, CRM does not give the option of installing an external assembly to the database. 

I am going to show another option for re-using common code, but first, let's talk about what the current options are.

Current Options

  • Use ILMerge to merge your plugin assembly with the external assembly(s). 
  • Sign the assembly and install the external assembly to the crm/bin folder on all CRM servers.  
    • This does the job, however, it won't work for plugins that run offline and it adds another step to deployment.  http://geekswithblogs.net/WesWeeks/archive/2009/02/23/129625.aspx
  • Sign the assembly and install the external assembly to the GAC (global assembly cache) on all CRM servers.

 

Another Option

Another option is to use a little-used feature in Visual Studio known as Link Files.  The files are located in another project and you add a link to them.  You can make changes to them and the changes are immediately reflected in both project.  You can also reference this external assembly in non-plugin assemblies (ASP.Net website, console applications, ect).  The main benefit is that the common code is compiled to the plugin assembly (similar to ILMerge).

Step 1:  Create a New Solution for Your External Assembly

Select File --> New --> Project

Specify the name and path of the new project to be used for common code.

 

Your new project has been created to hold your common classes.

 

Step 2:  Add Existing Plugin Project

Select File --> Add  --> Existing Project

Specify the path of the plugin project.

 

Your solution now contains both projects.

 

Step 3:  Add Plugin Project Folder to Hold Common Code

Add a new folder to hold a link to the common classes by right clicking on the project, selecting Add --> New Folder.  Rename the folder to Common Code.

 

The folder is now ready for you to add linked files.

 

 

Step 4:  Add Common Files

To add linked files, right click on the newly created folder and select Add --> Existing Item.

 

  • Select the file you would like to add from the ExternalAssembly project.  
  • Instead of just clicking "Add", click the drop-down icon next to the "Add" button and select "Add as Link".  This will add a reference to the file in the other project as opposed to just copying the source file.

Step 5:  Build

Build your solution.  If you get any errors, be sure that you have not added a reference to the external assembly.  We are not referencing it.  We are just linking to its files.

Note:  For non-plugin projects (ASP.Net projects, console apps, ect) do not use Link Files.  Instead, add a reference to the external assembly directly.

 

Enjoy!

-Andrew


Posted 03-27-2010 9:57 AM by Andrew Zimmer

Comments

Andrew Zimmer wrote re: Another Options for Referencing External Assemblies from Plugins
on 04-04-2010 9:59 AM

I had an offline question from a reader who asked, "Do you see any disadvantages to using this technique when developing plugins?"

I replied:

I find it to be the best solution available.  It provides the benefits of ILMerge without the pitfalls of deploying the actual external assembly.

The one problem I have seen is when your common assembly references a web service or a WCF service.  Your code can't see this reference because web service references are not available to link as a file.  I ran into this scenario when I had my common assembly reference the metadata service.  To fix it, I pulled out the code-generated file that VS creates and added it as a CS file instead of a web service reference.  Then I deleted the web service reference and linked up the cs file.  This was fine for my situation because the metadata service is static (don't need to do update web reference).  If you had a web reference that you regularly needed to update you can get around this by also adding the web reference to your plugin project as well.

Inetium, LLC. Site Information