[In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/pabloperalta] PLUGIN REGISTRATION TOOL ‘Unhandled Exception: System.BadImageFormatException: Could not load file or assembly’ - Pablo Peralta's Blog
Monday, November 22, 2010 10:38 PM pablop

PLUGIN REGISTRATION TOOL ‘Unhandled Exception: System.BadImageFormatException: Could not load file or assembly’

Running Visual Studio 2010 within Windows 7 64 bits I experienced this exception while trying to register an assembly into Dynamics CRM 4.

image

Error text:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'Microsoft.Crm.Sdk, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. An attempt was made to load a program with an incorrect format.

at System.Reflection.Assembly._GetExportedTypes()

at PluginRegistrationTool.AssemblyReader.RetrievePluginsFromAssembly(String path) in D:\pablop\Documents\DynamicsCRM\SDK\sdk13\sdk\tools\pluginregistration\AssemblyReader.cs:line 59

at PluginRegistrationTool.AssemblyReader.RetrievePluginsFromAssembly(String path)

at PluginRegistrationTool.RegistrationHelper.RetrievePluginsFromAssembly(String pathToAssembly) in D:\pablop\Documents\DynamicsCRM\SDK\sdk13\sdk\tools\pluginregistration\RegistrationHelper.cs:line 49

at PluginRegistrationTool.PluginRegistrationForm.btnLoadAssembly_Click(Object sender, EventArgs e) in D:\pablop\Documents\DynamicsCRM\SDK\sdk13\sdk\tools\pluginregistration\PluginRegistrationForm.cs:line 127

CAUSE

I found that the cause of the problem came from working with 64 bits. Honestly, not sure if it was due to my plugin assembly was written using 64 bits and the Plugin Registration Tool was compiled in 32 bits or vice versa.

Anyway…

THE SOLUTION

The solution I found was explicitly set ‘Platform Target’  = x86 both for Plugin Registration Tool and the plugin assembly project.

So, open the source code of the tool, located at the \tools\pluginregistration folder within Dynamics CRM SDK.

Right click on ‘Properties’ , click ‘Open’, go to ‘Build’ tab and select Platform Target to x86.

image

Repeat the same for your plugin project.

Then, run the tool again and try to register the assembly again.

It should work like a charm now Smile

clip_image001

clip_image002

clip_image003

Hope it helps to save somebody else time.

PP



Filed under: , ,

Comments

# Could not load file or assembly ‘Microsoft.Xrm.Client at plugin registration | stefan-scheller.com

Pingback from  Could not load file or assembly ‘Microsoft.Xrm.Client at plugin registration | stefan-scheller.com

# re: PLUGIN REGISTRATION TOOL ‘Unhandled Exception: System.BadImageFormatException: Could not load file or assembly’

Tuesday, April 05, 2011 4:39 PM by Shweta Mittal

Thanks for the solution. It came in very handy when I tried to register mine in a 64 - bit environment and faced the same issue. Saved me debugging time :)

Thanks

Shweta

Leave a Comment

(required) 
(required) 
(optional)
(required)