26

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?

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
SuperNES
  • 2,760
  • 9
  • 37
  • 49

11 Answers11

45

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.

For x86:

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

For x64:

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

Elliott
  • 2,479
  • 2
  • 21
  • 13
Darryl Braaten
  • 5,229
  • 4
  • 36
  • 50
  • how do I make this the defualt "installutil.exe"? – Louis Rhys May 30 '11 at 09:49
  • @louisrhys add the 4.0 framework directory to your path. You can modify your system path to have it for all cmd prompts or vcvarsall.bat if you just want to change if for the visual studio command prompt. – Darryl Braaten May 30 '11 at 14:01
  • 1
    @DarrylBraaten ... this is THE solution :) – Umer Feb 01 '12 at 12:41
  • 4
    Be careful with this, as you have to make sure you're targeting either x86 or x64 - in which case `...\Framework64\...` is NOT the correct path. For x86 you will be using this instead: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe`. If you try to use the incorrect architecture for your build you'll also get this error, even though the framework version is seemingly correct. – jocull Mar 07 '14 at 16:25
27

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:

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
SuperNES
  • 2,760
  • 9
  • 37
  • 49
  • I had the same problem when upgrading from .NET 2.0 to 4.0. In addition to it missing the Startup object, the Setup and Deployment project had .NET 2.0 referenced as a prerequisite. I needed to update the reference to .Net 4.0 and update my .NET Framework bootstrapper to 4.0 in the prerequisites dialog box. – xr280xr Apr 05 '11 at 18:15
  • I never understand MS "technology" :-(. Worked for me, thanks. – greenoldman Mar 16 '12 at 11:28
  • Another hint in addition to xr280xr's: If your Visual Studio runs on XP (x86) and you create an installer package for Windows 7x64 containing 32 bit code, on compilation you receive a warning telling you that the framework version does not match - you can ignore this message. Set up the installer for x64 CPU, but target the projects (your code) for x86 CPU or Any CPU (unless you have 32 bit 3rd party DLLs) to make it run correctly on Windows 7x64. That was the only way it worked for me... – Matt Oct 05 '12 at 13:18
15

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/

Stu
  • 2,426
  • 2
  • 26
  • 42
  • Thank you! Yes, I also converted a 2.0 project and didn't even find this until you pointed it out. – Nexxas Jan 23 '12 at 16:09
6

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

user3447136
  • 141
  • 1
  • 3
5

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.

oj_n
  • 51
  • 1
  • 1
  • 1
    My problem was that it was compiled for x64 but i was using the x86 installutil. this was a good tip – Justin Sep 19 '17 at 18:29
1

Having same issue, nothing from above was helping on windows server 2019

tried to run compatibility test, after that the registration suddenly worked: enter image description here

Kjeld Poulsen
  • 213
  • 3
  • 4
0

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
Alejandro del Río
  • 3,966
  • 3
  • 33
  • 31
  • My suspicion is it was neither the length of the path, nor the drive it resided on, but something like space characters in the name of a folder, which you could get to work by enclosing the service file path in double quotes. – Evgeniy Berezovsky Apr 10 '17 at 02:14
0

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.

BerggreenDK
  • 4,915
  • 9
  • 39
  • 61
  • ohhh someone else has commented too. didnt know – BerggreenDK Nov 18 '10 at 15:52
  • It's a .NET 4.0 project, and .NET 4.0 is installed on the machine. I'm trying to install on the same machine I've been coding on - I would think that if this were a problem, it would have cropped up earlier. – SuperNES Nov 18 '10 at 15:54
  • But you use the 2.0 install service? "\Framework64\v2.0.50727\installutil.exe" – BerggreenDK Nov 18 '10 at 20:50
  • I use this one: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe – BerggreenDK Nov 18 '10 at 20:52
  • I'm having the same problem. I upgraded my solution from .NET 2.0 to 4.0 and now the installer (from a setup and deployment project) gives me this error. Might the setup and deployment project be using the wrong version of installutil.exe? – xr280xr Apr 05 '11 at 17:45
  • 1
    That was not the case, see my comment under the answer above. – xr280xr Apr 05 '11 at 18:13
0

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.

Bernard
  • 7,908
  • 2
  • 36
  • 33
  • This doesn't generally work for .NET services, unless the author has manually added coded to handle the /install. These parameters used to be part of automatically generated code for older C++ services, so used to be common - not so anymore. – O'Rooney Oct 30 '14 at 03:41
  • @O'Rooney: You're most likely correct. Older Windows services are more likely to work this way. – Bernard Oct 30 '14 at 12:39
0

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.

JIANG
  • 1,687
  • 2
  • 19
  • 36
0

Wasted an hour on this,all I had to do was add quotes for the service path.

Step 1) cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

add the directory of the .exe path in quotes as shown

step 2) InstallUtil.exe "E:\MyNewService\MyNewService\bin\Debug\MyNewService.exe"

ref: https://www.codeproject.com/Answers/5143174/Installing-windows-service-error-installutil-exe-i#answer5

shyam
  • 1