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 lo开发者_如何学运维aded 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?
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
Nobody even came close to getting this one!
Here's what I had to do:
- Right-click the service project in Visual Studio, go to "Properties"
- Set "Startup object" to the name of the service (it had been set to the value "(Not Set)").
- Save.
- Build
- Try to install again.
- It works! Yay! We can all go home!
Link to the code project article that helped:
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/
For me I just right right clicked on the exe I was trying to install and clicked "Unblock"
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.
Having same issue, nothing from above was helping on windows server 2019
tried to run compatibility test, after that the registration suddenly worked:
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.
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
精彩评论