在 Visual Studio 2012 中附加到某些进程时,我收到了这样的错误:
完整的异常如下所示:
System.BadImageFormatException: Could not load file or assembly 'C:\Program Files (x86)\SDK\Serv\bin\Debug\Service.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'file:///C:\Program Files (x86)\SDK\Serv\bin\Debug\Service.dll'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
这种问题的原因是什么以及如何避免它?
更新以提供更多详细信息
在程序集绑定日志查看器(Windows SDK 中的 FUSLOGVW.exe)中启用日志记录时,日志显示:
The operation failed.
Bind result: hr = 0x8007000b. An attempt was made to load a program with an incorrect format.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\WcfSvcHost.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = EUROPE\JAWAL
LOG: Where-ref bind. Location = C:\Program Files (x86)\SDK\Serv\bin\Debug\Service.dll
LOG: Appbase = file:///C:/Program Files (x86)/SDK/Serv/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files (x86)\SDK\Serv\bin\Debug\Service.dll.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/SDK/Serv/bin/Debug/Service.dll.
LOG: Assembly download was successful. Attempting setup of file: C:\Program Files (x86)\SDK\Serv\bin\Debug\Service.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Service, Version=6.100.4434.0, Culture=neutral, PublicKeyToken=null
ERR: Invalid assembly platform or ContentType in file (hr = 0x8007000b).
ERR: Run-from-source setup phase failed with hr = 0x8007000b.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
服务定义如下:
[ServiceContract(SessionMode = SessionMode.Required)]
[ServiceKnownType(typeof(TypeInfo))]
public interface IServiceContract
{
[OperationContract]
[FaultContract(typeof(ServiceFault), Name = "ServiceFault")]
string GetConfiguration();
[OperationContract]
[FaultContract(typeof(ServiceFault), Name = "ServiceFault")]
void SaveConfiguration(string config);
[OperationContract]
[FaultContract(typeof(ServiceFault), Name = "ServiceFault")]
byte[] GetImage(int x, int y);
[OperationContract]
[FaultContract(typeof(ServiceFault), Name = "ServiceFault")]
IList<ITypeInfo> GetSupportedTypes();
}
[DataContract(Namespace = Namespaces.Data)]
public class ServiceFault
{
private string _message;
[DataMember]
public string Message { get { return _message; } set { _message = value; } }
public ServiceFault() { }
public ServiceFault(string message) { _message = message; }
}
public interface ITypeInfo
{
string Type { get; }
string Description { get; }
}
public class TypeInfo : ITypeInfo
{
public string Type { get; set; }
public string Description { get; set; }
}