Question

So I built a service in C# and I am trying to use the following command to install it:

C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\installutil.exe MyService.exe >> installLog.txt

It fails. When I look at the installLog.txt, I get this:

Microsoft (R) .NET Framework Installation utility Version 2.0.50727.3053
Copyright (c) Microsoft Corporation.  All rights reserved.

Exception occurred while initializing the installation:
System.BadImageFormatException: Could not load file or assembly 'file:///C:\MyService.exe' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded..

The same approach works fine for installing a different assembly. I feel like it might be because the one that fails was written for .NET 4.0, and the one that works is in 3.5.

Does anyone have any experience with this problem?

Was it helpful?

Solution 2

Nobody even came close to getting this one!

Here's what I had to do:

  1. Right-click the service project in Visual Studio, go to "Properties"
  2. Set "Startup object" to the name of the service (it had been set to the value "(Not Set)").
  3. Save.
  4. Build
  5. Try to install again.
  6. It works! Yay! We can all go home!

Link to the code project article that helped:

OTHER TIPS

You are using the wrong installutil.exe If your application is built against .Net 4.0. Use the the installutil.exe in the 4.0 folder.

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe

For those of you who don't find that this fixes their problem you need to look in the Launch conditions of the .vdproj file and change the .NET framework to 4: http://www.alexjamesbrown.com/uncategorized/deploying-net-4-project-error-1001-system-badimageformatexception/

If you compile on x64 machine, be sure to go into Configuration Mgr and change the build to x64. Of course you'd want to check to make sure you're using the right version of InstallUtil.exe. g'luck.

For me I just right right clicked on the exe I was trying to install and clicked "Unblock"

I believe you provide the answer in your question:

"This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.."

So you might have compiled the runtime with newer functions (which are in newer Frameworks) but the computer you are registrating it on is running a lower version.

Eg. compile for .NET 3.5 but install the service afterwards on a .NET 2.0 wouldnt make sense.

Since you are able to install services on the destination machine, I guess you already are admin. So just upgrade the .NET framework on that service-machine.

Have you tried simply typing MyService.exe /install in a Command Prompt window in the directory containing your Windows service? That has worked for me in the past. You can also uninstall a Windows service by using MyService.exe /uninstall.

I had the same problem. Initially my project was in a very long path on D:\ inside the debug folder of my project. The problem was solved when I moved the .exe service file to another location with a shorter path and started with "C:\".

I also use this batch file to install the my services

@ECHO OFF

REM Directory for .NET 4. Check for other version directories.
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i C:\ServiceFolder\MyService.exe
echo ---------------------------------------------------
echo Done.
pause

If you tried all the above and still see the same error, double check if you have a 32-bit application or any 32-bit DLLs and your server is a 64-bit server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top