Web service calls 32 bit dll function - c#

I have created web service that uses some external dll. Evrything works fine when I run web service on IIS Express from VS IDE. Web service call dll functions without problems. But when I publish this web service to local computer IIS and call method that exposes dll function I got error:
System.BadImageFormatException: An attempt was made to load a program
with an incorrect format. (Exception from HRESULT: 0x8007000B)
According information I found on internet it might bee 64/32 bit compatibility issue. My dll is compiled like 32 bit app.
How to fix this problem?

In IIS go to the Application Pools.
Select the Application Pool of your web service then click on "Advanced Settings".
Look for the option "Enable 32-Bit Applications" and set it to true.
Then restart IIS and try again.
I had a similar problem and I solved in this way

Related

why does a 32-bit value cause a WCF System BadImageFormatException?

I published a small web application with WCF services locally by using a File System in Visual Studio Community 2017. I hosted it locally to test it and I noticed that a single WCF service was not called and raised a System.BadImageFormatException. The reason was a single id variable from a method of Int32 type.
The solution platform in the Configuration Manager, is a "Mixed Platforms" with Any CPU. So the problem was not there.
I followed the solution by bluwater2001, here by enabling 32 bit applications in the Application pool. However my question remains: Why a 32-bit variable caused such an error whereas my web app was built for any cpu and mixed platforms? And why was it necessary to enable 32-bit applications in the application pool in IIS?
This has nothing to do with the type of your variables/parameters.
If you choose AnyCPU your code can run on any platform. It will compile into 32bit code on a 32bit machine and into 64bit code on 64bit machine.
If your assembly is running as a 32bit process it cannot load a 64bit assembly and vice versa. If it tries to load a assembly which doesn't match the process you get a BadImageFormatException.

DLL not found on IIS Remote Server C# WCF

I have a C# WCF Web Service on a remote server running on IIS 8.5. One of the method of this web service tries to call a function in another custom DLL on the remote server but it returns a System.Exception specifying that it doesn't found the DLL. I logged the exception on the DLL Loader and it returns the right path to the DLL though.
The DLL is a C++ DLL used to execute a Matlab function via the runtime (installed on the server and referenced in the PATH variable as well as the DLL.
My DLL is situated in the bin folder of the project. I also tried to put it into the %windir%/System32 but same error.
By the way, the web service works great while on my Local Computer. The error only occurs while deployed
Any idea on how I can have my web service to find the DLL and execute the function?
Thanks,

Using 32 bit C library in published .Net project on Windows Server 2012

I am using graphviz as library in my MVC project. To be more specific I am doing dll import 32 bit C library as gvc.dll and cgraph.dll like here.
As I debug my project locally it works fine. However, after web deploying it to my Windows Server 2012 (that is a 64bit OS) it fails to load those dlls. I configured a remote debugger to see that is the case. After if tries to get a Context (gvContext()) it crushed with exeption: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B). My IIS is of 6.2 version.
I need to note that generating images from graphviz using cmd on my windows server works just fine. So I assume that Path variables are correct.
I have found different solutions that I have tried:
enable 32bit application
doing a web deploy with following configuration: Release for x86
running the 64 bit version of IIS Express
Nothing worked. I cannot even check if the error has changed as remote debugger doesn't work after I enabled 32bit application.
Any ideas what is wrong?

ASMX to COM call in 64 bit Server

I am trying to call the COM library method(COM interopablity) from ASMX webservice hosted in IIS 8.5 & Windows Server 2012 64 bit server. But the call doesnt respond. I have done the following changes to ensure we are doing the right setup
1. Enable 32 bit application = true for application pool setup
2. Registered the COM in the following locations
C:\Windows\System32 & C:\Windows\SysWoW64
3. Tried compile the webservice project to target X86 configuration
4. Changed the execution timeout for web service to 10 mins
5. Did setup application pool identity which worked for my windows application
But none of the above works.
Just to verify .Net and COM communication, I have created the windows
application to call the same method and it works when I compile the
project in X86 platform.
If I compile the windows application to target platform to Any CPU, I
get the same issue like calling it via ASMX web service. Its not
responding to the call.
Finaly, It looks like it is working for windows application target x86
platform. But no idea, why it is not working from asmx service when I
set Enable 32 bit application.
Could you help me to identify what could be the wrong with ASMX to COM call?
Referenced:
Error 80040154 (Class not registered exception) when initializing VCProjectEngineObject (Microsoft.VisualStudio.VCProjectEngine.dll)
How to repair COMException error 80040154?
Note: I was getting COM exception 80040154 error when COM was not installed on C:\Windows\System32 folder. Now not getting this error but call not responds.
Server is 64bit
Depending on the COM server you invoke, you may need to specify an Identity for an Application Pool if the COM application requires a user profile, or check the app pool's Load User Profile property. (I needed this to run ABCpdf, for example)

How to call unmanaged code in ASP.NET website and host it in IIS?

I have created a C++ dll. It works fine and does the job.
I have created methods in asp.net that call the methods in C++ dll. DLL path is absolute. It works fine too.
I then published the website onto a folder and hosted the website on IIS. I get an error message
Exception Details: System.DllNotFoundException: Unable to load DLL 'FilesCreator.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Please suggest what I need to do to get it to work.
Did you confirm that the authenticated user of the application pool which is running your app has the appropriate permissions to read and execute the DLL?
Try adding permissions for the user who is running the application pool for IIS7. For IIS6, if you are using anonymous authentication trying giving permission to the IIS anonymous user as configured in your Web Site settings.
See here for IIS6 and here for IIS7.

Categories

Resources