CRM Blog

GAC IT! Developing Portable Code For Microsoft CRM.

by Danny Varghese 03.05.08
Intro

As a developer, we’re faced with many challenges outside of trying to solve the problem we’ve been given.  One of these is how to create portable code, i.e. code that can be re-used, debugged and changed easily.  There have been many situations where the same action needs to be taken, but from different sources.  For example, connection to the database, executing a stored procedure or logging data to a log file may need to be called numerous times, but by different sources.  This is especially true within CRM because development files may need to exist in different directories so that CRM can access them appropriately. 

Usage

To help with this in .NET, you can create an assembly file that contains all classes and methods that need to be re-used (portable) by other files, and then you can GAC it!  “GAC” stands for Global Assembly Cache, which is a specially controlled central repository of shared assembly files.  Assemblies that reside in the cache must adhere to a versioning policy which can be achieved by strong naming.  Strong naming assigns a strong key which uniquely identifies a component, thus maintaining different versions of the same file.  Once the file has been created and strong named, you’re ready to upload the file in the global assembly cache.  The gacutil.exe is the .NET utility that is used to work with the GAC.  You can check the availability of the assembly file in the GAC by using the command “gacutil.exe /l <name of assembly file>.”  To register the file, use the command “gacutil.exe /i <name of assembly file>.” 

General Example

An example of how the GAC would be used by others would be if you have an assembly file called “Test.dll” but its version 1.0.  It’s not possible to compile another version, say 2.0, called “Test.dll” in the same directory. However, using the GAC, one can upload different versions of the same file and other files can reference those specific versions. 

CRM Specific Example

An example of how a CRM developer would use it is say you have two assembly files located in different directories.  Say both assembly files need to connect to a database to retrieve information.  You can create an assembly file that has methods to connect to the database and perform other operations.  Then strong name/type that file (an example of strong typing can be found at: http://support.microsoft.com/kb/815808).  Once strong typed, you can add it to the global assembly cache (GAC command line options: http://msdn2.microsoft.com/en-us/library/ex0ss12c(VS.71).aspx).  Once added, open the projects that need to reference the newly “cached” assembly file in Visual Studio.  Add a new reference, and this newly added file should show up in the list of .NET files you can reference.

Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Security Check
Please answer the simple math problem below.

(required)