6

I have implemented windows service using .Net Core worker service. When installing service from command prompt, getting error

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe D:\LC\WindowService\L3WorkerService.exe
Microsoft (R) .NET Framework Installation utility Version 4.8.3752.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Exception occurred while initializing the installation:
System.BadImageFormatException: Could not load file or assembly 
'file:///D:\LC\WindowService\L3WorkerService.exe' or one of its dependencies. 
The module was expected to contain an assembly manifest..

I have tried with these two path

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe D:\LC\WindowService\L3WorkerService.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe D:\LC\WindowService\L3WorkerService.exe

This is my project information

  • Platform target - x86
  • Output type - WindowsApplication
  • Target Framework - .NET Core 3.1

How can I fix this issue?

R15
  • 13,982
  • 14
  • 97
  • 173
  • 3
    Why do you use `InstallUtil.exe` from .NET FW to install a .NET Core service? – Pavel Anikhouski Jun 04 '20 at 11:00
  • @PavelAnikhouski - I am new to .Net, just followed articles to install service. How should I do it? Thank you. – R15 Jun 04 '20 at 11:02
  • You can google a little bit to find some msdn articles, like [Host ASP.NET Core in a Windows Service](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-3.1&tabs=visual-studio) and follow them – Pavel Anikhouski Jun 04 '20 at 11:05

1 Answers1

10

To install service need to follow below steps

open cmd as admin and switch to output/publish dir of project. Then type

sc create TestService BinPath=D:\LC\WindowService/TestService.exe

You will get message [SC] CreateService SUCCESS. Other commands also available including start service

sc start TestService
sc stop TestService
sc delete TestService

Note: In .Net Core services avoid using InstallUtil.exe.

R15
  • 13,982
  • 14
  • 97
  • 173