Error when browsing a custom service hosted as IIS web service: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Title: Error when browsing a custom service hosted as IIS web service – An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Scenario:

You are using Microsoft Dynamics AX 2012
You have created several Custom Services that you can deploy using NetTcp successfully and consume the WSDL in your client applications without any errors.

You can deploy the same Custom Services in an enhanced port, using the HTTP adapter (to host as IIS Web Services).
However when you try and browse to the service you get an error similar to the below:

Server Error in ‘/MicrosoftDynamicsAXAif60’ Application.
——————————————————————————–
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
Stack Trace:

[BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)]
   System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType) +0
   System.Reflection.RuntimeMethodInfo.get_Signature() +82
   System.Reflection.RuntimeMethodInfo.GetParameters() +38
   System.Runtime.Serialization.ClassDataContractCriticalHelper.EnsureMethodsImported() +189
   System.Runtime.Serialization.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) +786
   System.Runtime.Serialization.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) +117
   System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type) +85
   System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory) +413
   System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription operation, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy) +308
   System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) +69
   System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch) +120
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +4122
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +90
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +182
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +742
   System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +126
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +901
[ServiceActivationException: The service ‘/MicrosoftDynamicsAXAif60/<your custom service name>/xppservice.svc’ cannot be activated due to an exception during compilation.  The exception message is: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B).]
   System.Runtime.AsyncResult.End(IAsyncResult result) +650220
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +210733
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +166
 

——————————————————————————–
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055

 

Possible Causes:

Cause 1:

One possible reason can be that the Custom Services Data contract classes contain other methods referencing types that are not part of the service/data contract.  Data contract classes should only have parm methods that are the data members. So when deploying the service to IIS, the modules containing these other types are not included and the types do not load. 

When generating service classes, we only parse the public service and data contracts.  We expect that data contracts are just containers for passing data around and do not have a dependency on non-service contract types.  When deploying to IIS, we only copy the types that are part of the contract.  This has always been the design for using data contracts in Services and AIF. 

 

Resolution:
To resolve this, re-view your service contracts and refactor the code and remove all parm methods from data contracts that are not data members, and redeploy the service to IIS again.

 

More information:
The reason why we don’t experience the same behaviour when consuming the Custom Services over  NetTcp is that NetTcp is hosted on the AOS where all data types and NetModules are available and it manages to resolve the other data types. The real solution is to ensure that Data contract classes only have parm methods that are the data members. Further info on using Data Contracts in X++ can be found at http://technet.microsoft.com/library/gg848068.aspx

 

Cause 2:

Assuming the data contract services are all valid, and you are running Microsoft Dynamics AX 2012 R2 with application build 6.2.1000.4051 (CU-7), then review and/or test with Application hotfix KB2934017