0
votes

I have a custom plugin registered in my CRM 2011 instance. Until today the plugin was working perfect but al of a sudden I am get this System.BadImageFormatException error when creating a record. I have tried researching the issue but I can only find people having this issue while registering a plugin. My research has led me to believe it is something to do with 64 bit vs 32 bit. I have tried changing the target of my assembly from "Any CPU" to 64 bit but I get the same error when the plugin executes. The error details are below. Thanks in advance.

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.BadImageFormatException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #6C9D565ADetail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220970</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>System.BadImageFormatException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #6C9D565A</Message>
  <Timestamp>2016-03-17T19:05:45.6106609Z</Timestamp>
  <InnerFault i:nil="true" />
  <TraceText>

[PluginProfiler.Plugins: PluginProfiler.Plugins.ProfilerPlugin]
[0b8f5ab6-66ec-e511-8367-000c29e9a79c: MultiFamily.PostHomeCreate: Create of anh_home (Profiler)]


</TraceText>
</OrganizationServiceFault>
2

2 Answers

1
votes

If you set it to "Any CPU" it should pick up the architecture (x86, x64) fine.

It looks like the architecture of the plugin profiler (how pluginregistration was compiled) and your plugin don't match. BadImageFormatException is normally raised by a x86 assembly trying to load a x64 one or viceversa.

1
votes

I managed to resolve my issue but I'm still not sure what the cause was. I wasn't 100% sure that the error wasn't related to the plugin code so I commented out all the lines of code (save the lines required to register and execute the plugin), compiled and updated the plugin via the plugin registration tool. I then triggered the plugin execution from inside CRM and low and behold the plugin executed with no errors. Now I just had to find the line of code that was causing the error. This is where things got weird.

Since the fully implemented plugin was throwing errors I was unable to profile and debug the plugin so I resorted to restoring functionality to the plugin line by line. Every time I would restore some functionality I would build, update the assembly and test the plugin execution from CRM. Oddly, I was able to restore all the original functionality, build, update and test the plugin without any errors.

I am not sure what might have gone wrong but absolutely nothing had changed but I now have a fully functional plugin. Lesson learned, when nothing else works, everyone out of the pool.

Thanks Jordi for your time and comments hopefully they can help someone more than they helped me.