System.BadImageFormatException: Could not load file or assembly - windows

I have developed a Windows Service application using Visual Studio 2010 in order to integrate two applications.
In my WinService, I call some API DLL files provided by the destination application developer to create some information in the destination application after getting that information from the origin application.
So my windows service works like some middleware application.
When I run the service I get the following error:
System.BadImageFormatException: Could not load file or assembly 'Interop.ErpBS800, Version=8.5.0.0, Culture=neutral, PublicKeyToken=e076e239d0e78a42' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'Interop.ErpBS800, Version=8.5.0.0, Culture=neutral, PublicKeyToken=e076e239d0e78a42'
at MaeilKitWintouch.Primavera.CreateDocument(Facturas fac)
at MaeilKitWintouch.MaeilKitWintouch.ExecuteRequest()
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].
In my Windows 7 x86 laptop everything works great, but in the Windows Small Business Server 2011 Essentials 64-bit I get that error.
I have PLATFORM TARGET = Any CPU and TARGET FRAMEWORK = .NET 4.

Your application is likely running in 64-bit mode (which is allowed because you set the Platform Target to Any CPU) and trying to load a 32-bit library.
The blog post Compiling .NET for a Specific Target Platform (Any CPU vs x86 vs x64) summarizes it well.

My solution for this error was kind different, after 4 days of 'platform war', if I change to x86/x64 I got the same error....
What I did was:
Unload from visual studio all projects and references to the DLL in conflict (also comment all code lines with references), the project must run without errors.
In the main app, go to properties -> compile -> advance compile options -> Tarjet CPU - and in here change to x86
Re compile project
Load project/refrences to DLL
Test if works fine
Uncomment all code lines and recompile

Related

Unit Test Adapter threw exception: Unable to load one or more of the requested types

I am attempting to run SpecFlow tests from the Visual Studio 2010 Command Prompt, and I am getting a rather obtuse error message:
Unit Test Adapter threw exception:
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information..
Some info about my VS2010 Project setup:
Windows 7 Enterprise, 64 bit (Version 6.1.7601 Service Pack 1 Build 7601)
Visual Studio 2010 Premium (v10.0.40219.1 SP1Rel)
Using Coded UI Tests
Using SpecFlow 1.9.0, which delegates to the CodedUI test API
MSTest
.NET v4.0.30319
Whole solution is compiling to 32 bit code (I have coworkers using XP still)
I have a post-build event that copies a few DLL files from my NuGet packages directory to the target dir:
copy $(SolutionDir)packages\SpecBind.1.2.1.71\lib\net45\SpecBind.dll $(TargetDir)
copy $(SolutionDir)packages\SpecBind.CodedUI.1.2.1.71\lib\net45\SpecBind.CodedUI.dll $(TargetDir)
Without this, mstest somehow couldn't load a number of SpecFlow assemblies.
Relevant Parts of Test Project's App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
<section name="specBind" type="SpecBind.Configuration.ConfigurationSectionHandler, SpecBind" />
</configSections>
<connectionStrings>
...
</connectionStrings>
<specFlow>
<!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
<unitTestProvider name="MsTest" generatorProvider="Specflow.CodedUI.MsTestCodedUiGeneratorProvider, Specflow.CodedUI" runtimeProvider="TechTalk.SpecFlow.UnitTestProvider.MsTest2010RuntimeProvider, TechTalk.SpecFlow" />
<stepAssemblies>
<!-- This attribute is required in order to use StepArgument Transformation as described here;
https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Step-argument-transformations -->
<stepAssembly assembly="SpecFlow.Assist.Dynamic" />
<stepAssembly assembly="SpecBind" />
</stepAssemblies>
</specFlow>
<specBind>
<browserFactory provider="SpecBind.CodedUI.CodedUIBrowserFactory, SpecBind.CodedUI" browserType="IE" />
<!-- For additional details on SpecBind configuration options see the project site. -->
</specBind>
</configuration>
The command I was using to start the tests:
C:\path\to\bin\Debug> mstest /testcontainer:MyTests.dll /test:SpecFlowFeatureName
Loading MyTests.dll
Starting Execution...
Results Top Level Tests
-------- ------------------
Failed ...
Failed ...
Failed ...
...
I've been searching high and low for a solution, and all I keep finding is references to VS2008 and disabling Code Coverage. I have VS2010 and Code Coverage is not enabled in my local test settings.
Windows Event Log Viewer
After rooting around in my Windows Event Log viewer, I finally came across more info (I've heard people complain they can't find a stack trace with this error -- look in your Event Viewer)
The description for Event ID 0 from source VSTTExecution cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
(QTAgent32.exe, PID 6920, Thread 213) Unit Test Adapter threw exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.GetAssemblyInfo(Assembly assembly)
at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.GetTypeInfo(Type type, Boolean checkAlreadyExaminedType)
at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.ResolveMethods()
at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.Initialize(UnitTestResult result, UnitTestRunner runner, ConsoleOutputRedirector redirector)
at Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestRunner.Run(UnitTestElement test, ITestContext testContext, Boolean isLoadTest, Boolean useMultipleCpus)
the message resource is present but the message is not found in the string/message table
I don't know how to resolve this error. SpecFlow tests run fine from inside Visual Studio when I right-click in a .feature file loaded in the editor and choose "Run SpecFlow Scenarios..." but I can't run them from the command line.
Any ideas, or additional information I could post?
As a side note, this stemmed from my earlier question: How do you run SpecFlow scenarios from the command line using MSTest?
Update #1
I did a little more poking around, and found this blog post: MSTest and 64bit. I checked my local test settings, and it is forcing the tests to run in 32 bit.
Now using this command:
mstest /testcontainer:MyTests.dll /testsettings:"..\..\..\Local.testsettings"
Error message:
Test method Blah.Dee.Blah threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.Extension, Version=11.0.0.0, Culture=neutral, PublicKeyToken=...' or one of its dependencies. The system cannot find the file specified.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].
TestCleanup method Blah.Dee.Blah.ScenarioTearDown threw exception. BoDi.ObjectContainerException: BoDi.ObjectContainerException: Interface cannot be resolved: SpecBind.BrowserSupport.IBrowser.
Update #2
I removed the reference to Microsoft.VisualStudio.TestTools.UITest.Extension and added it back. Cleaned. Rebuilt. Ran the same command again. Still getting the same error.
For some reason it's trying to load the Microsoft.VisualStudio.TestTools.UITest.Extension assembly as Version 11, and I have Version 10 installed on my machine. I'm trying to track down where this version is being specified so I can change it.
Just in case anyone else comes across this, for me it was because code coverage was enabled. Disabling it resolved the issue:
http://blogs.msdn.com/b/danielvl/archive/2010/02/16/enable-code-coverage-in-visual-studio.aspx
Open the .testsettings file
Under "Test Settings", click "Data and Diagnostics"
Uncheck "Code Coverage"
Hit "Apply"
With the help of #jessehouwing's comment above, I've got this fixed. I was running into two problems.
The error I originally referenced was actually caused by mstest attempting to run the tests in a 64bit process (QTAgent.exe). I needed it to run as a 32 bit process because the project was compiled as 32 bit code on my 64 bit Windows 7 machine.
In Visual Studio 2010:
Click the "Test" menu -> "Edit Test Settings" -> "Local (local.testsettings)"
Click "Hosts" in the left hand panel
Make sure "Force tests to run in 32 bit process" is chosen in the "Run tests in 32 bit or 64 bit process" drop down. For me this was the default
In the Visual Studio Command Prompt, run the tests by referencing the local.testsettings file generated by Visual Studio:
mstest /testcontainer:foo.dll /testsettings:"path/to/solution/local.testsettings" /test:SpecFlowFeatureName
The second problem I had was my own fault. Earlier I was playing around with Coded UI tests and SpecFlow and had installed SpecBind, which threw a bunch of settings in App.config for my test project. This NuGet package is only for .NET 4.5, and we use .NET 4.0. I removed that NuGet package and settings from App.config, cleaned and rebuilt. This fixed this issue completely.
So in short, I:
Forced mstest to run in a 32 bit process, and referenced this local.testsettings file from the command line
Removed all references to SpecBind because I got a little NuGet-happy earlier and installed a package incompatible with my .NET runtime.
As you can see by looking at the assembly references required by these packages (I use Reflector for this, but IlSpy or JetBrains' DotPeek will work as well), you'll see that they depend on the v11 version, which is the version that ships with Visual Studio 2012. These can't be used in Visual Studio 2010.
Removing SpecBind will probably help, or finding a version that depends on an older version of CodedUI (you're probably looking for a much older version of SpecBind in this case).
As for your 32-bit issue, you should probably set your project's CPU configuration to AnyCPU, there is normally no need to fix it to x86, unless you're dependent on native methods or if you're referencing other assemblies that are specifically set to use x86 as their target architecture.
This is so troubling.....
I had Test Agent for Visual Studio 2015 installed on my test machine, since we were planning to use it - while on my local machine I had 2013. The tests ran so perfectly with MsBuild on local, that I couldn't understand what could have happened given that I always assumed Microsoft always maintained backward compatability.
MsBuild on test server failed to give any information as to what assemblies were missing (I added Newtonsoft.Json just because it complained initially, but after that I never saw the names of anything else missing). The error message was truncated, stacktrace and debugtrace options never worked...
Once I installed Test Agent for Visual Studio 2013 on the test machine, it ran like a charm.

Run Visual Studio 2013 solution with Crystal Reports

I have an MVC application originally developed in VS2010. We have Crystal Reports (CR) integrated into it and all works smoothly.
Now, I need to setup some new workstations which will have only VS2013. I can run the site in VS2013, but only because I have VS2010 and previously installed the CR runtime files. The new stations won't have VS2010 installed and the SAP installer will not run unless VS2010 is on the machine. SAP will not provide a fix until early 2014 at the earliest.
The new workstations don't need to be able to modify the reports, so I don't care if the full CR application is installed, I just need it to be able to reference the runtime files so they can debug other areas of the application.
I cannot remove CR from the application at this time, and cannot install VS2010 on all the new workstations first.
I have tried putting the dlls in the bin folder of the site. I have tried putting them in C:/Windows/Microsoft.NET/
Framework/v4.0.30319/Temporary ASP.NET Files/root/dec0de27/fa4bed84/ Nothing seems to work.
Where do I place the dlls so VS2013 can use them? I know there has to be some way to do this because my workstation can run the site in VS2013 eventhough SAP hasn't released a 2013 version.
Here is the error when they try to run the applicaiton:
Server Error in '/' Application.
Could not load file or assembly 'CrystalDecisions.ReportAppServer.ClientDoc'
or one of its dependencies. An attempt was made to load a program with an
incorrect format.
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: Could not load file or
assembly 'CrystalDecisions.ReportAppServer.ClientDoc' or one of its
dependencies. An attempt was made to load a program with an incorrect format.
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.
Assembly Load Trace: The following information can be helpful to determine why
the assembly 'CrystalDecisions.ReportAppServer.ClientDoc' could not be loaded.
=== Pre-bind state information ===
LOG: DisplayName = CrystalDecisions.ReportAppServer.ClientDoc
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: CrystalDecisions.ReportAppServer.ClientDoc | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is
provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the
assembly,
WRN: that consists of the simple name, version, culture, and public key
token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more
information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/Office/Documents/GIT/Site/site/
LOG: Initial PrivatePath = C:\Users\Office\Documents\GIT\Site\site\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file:
C:\Users\Office\Documents\GIT\Site\site\web.config
LOG: Using host configuration file:
C:\Users\Office\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from
C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom,
partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/
Framework/v4.0.30319/Temporary ASP.NET Files/root/dec0de27/fa4bed84/
CrystalDecisions.ReportAppServer.ClientDoc.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/
Framework/v4.0.30319/Temporary ASP.NET Files/root/dec0de27/fa4bed84/
CrystalDecisions.ReportAppServer.ClientDoc/
CrystalDecisions.ReportAppServer.ClientDoc.DLL.
LOG: Attempting download of new URL file:///C:/Users/Office/Documents/GIT/
Site/site/bin/CrystalDecisions.ReportAppServer.ClientDoc.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing
terminated.
This is the SAP discussion http://scn.sap.com/message/14614675#14614675
Crystal report is updated support for VS 2013
Updated version document
Probably no longer relevant to you, but CR for VS2013 is now available in support pack 9. Note: You have to run the exe to get integration with VS IDE.
Download from: http://scn.sap.com/docs/DOC-7824
Well, I finally found an answer.
If you install the CR x86 Redist MSI package (for some reason it HAS to be 32bit) it will allow you to run VS2013 without throwing an error. You won't be able to modify the reports, but at least you can build your application.
Download the latest here:
http://scn.sap.com/docs/DOC-7824
Straight from the horse's mouth:
CR for VS 2013 will not be available until early 2014. Please see this thread and the referenced threats in it for more info.

Missing System.Data.SQLite.dll reported after installation

The desktop software (.Net 4.0, compiled under Visual Studio 2010, target: x86) I'm developing is shipping with System.Data.SQLite (1.0.86 for x86, from system.data.sqlite.org*) for the first time.
On the development machine (And every other machine where the System.Data.SQLite-installer has run), everything works correctly.
On the testing machine however, after installation (a standard MSI setup created with the Visual Studio Installer) launching the application and showing the splash screen, before showing the GUI, the application creates an exception with the error:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.SQLite.dll' or one of its dependencies.
The mentioned file itself is found in the application directory (correct version), along with all the other referenced DLLs.
I assume this works on the development machines because of the setup option 'Install images'. I didn't install SQLite in the GAC. The application project is directly referencing the pre-build System.Data.SQLite.dll with local copy set to true.
Are there any dependencies I'm not seeing?
Or is it a missing Microsoft Visual C++ Runtime Library (Not present in the application directory)?
Or a missing SQLite.Interop.DLL (Not present in the application directory)?
How could I fix those?
= The installer I've used: http://system.data.sqlite.org/downloads/1.0.86.0/sqlite-netFx40-setup-bundle-x86-2010-1.0.86.0.exe
Ensure that you have the latest Microsoft Visual C++ 2010 Redistributable Package installed.
http://www.microsoft.com/en-gb/download/details.aspx?id=5555
Why? The sqlite DLL has some unmanaged code embedded in it that is run directly from the assembly at runtime. This manifested for me as a bad image format exception but you never know!

Visual Studio 32-bit & 64-bit Strong Assembly Name Issue

Trying to build a project that uses managed C++/CLI and it compiles successfully when in the 64-bit build config. When I switch to Win32 config it fails siting the managed C++/CLI dll is not a strong named assembly. When switching configs I have verified that both have the Project -> Configuration Properties -> Linker -> Advanced -> Key File entry has the proper path to the snk file ($(SolutionDir)key.snk)
Not sure if I'm missing something here. I haven't been able to find anything on Google...
[UPDATE]
If I use a post build event in the C++/CLI project the output window shows the following error:
c:\path\file.dll does not represent a strongly named assembly
I'm using the following post build event:
sn -Ra "$(TargetPath)" "$(SolutionDir)key.snk"
Again - the above post build event works on the x64 config but not the win32. (I have tried compiling on both 32-bit & 64-bit machines w/ same result)
Just ran into the same problem today, after some digging it looks like this is caused by a bug introduced in Microsoft Visual Studio 2010 SP 1.
A description of what caused the bug and a workaround can be found at the following link:
http://blogs.msdn.com/b/vcblog/archive/2011/03/11/10140139.aspx

exception HRESULT: 0x800700C1 when executing c# application referencing native c++ lib with boost

I am running a C# application that references a C++\CLI wrapper project which in turn references a native c++ project dependent on Boost 1.47 library (links to files of the form ...vc100-mt-gd-1_47.lib)
All libraries are statically linked and everything compiles great. Executing the C# app results in an exception:
"...is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)." claiming some dependency is missing somewhere.
C# application is configured to x86 platform while all other projects are Win32, including boost's binaries.
EDIT: before adding the use of Boost library, it DID work
How can I track down the problem?
It is known issue. The reason is Thread Local Storage (TLS) used in the Boost::Thread. To fix it you should either disable libboost_thread-vcXXX-mt-1_XX.lib and force linker to link your C++/CLI assembly with boost_thread-vcXXX-mt-1_XX.lib (stub of the boost_thread-vcXXX-mt-1_XX.dll).
Or you can create your own DLL and link it with libboost_thread-vcXXX-mt-1_XX.lib. Then you can link your C++ DLL with C++/CLI assembly.
See
Using boost threads when compiling with /clr
Boost Threads with CLR
Using boost in CLR
Came across this nasty error the other day whilst trying to get an IIS app running on my Visat 64bit PC:
Error: Is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)
Solution:
Within IIS 7 click on Application Pools (left hand side under {machine_name})
Select your app pool and then click Advanced Settings on the right.
Second setting in the list: Enable 32-Bit Applications - must be set to True.

Resources