PLUGIN REGISTRATION TOOL ‘Unhandled Exception: System.BadImageFormatException: Could not load file or assembly’
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.
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
Hope it helps to save somebody else time.
PP