The complete steps of deploying. Net core to Windows Service

Time:2020-5-18

preface

Net core project department has many ways in windows, including the following,

  1. Dotnet command,
  2. iis(windowshosts),
  3. Some open source application containers (dockers)
  4. Based on some exe programs, Microsoft official website cases
  5. NSSM building windows service

Among them, the advantages and disadvantages are not discussed here. I built it on the windows service today. This way is relatively beautiful for our existing dotnet (one black frame, one can’t see), and the computer can restart automatically after restarting.

1. Implementation

One point one . download NSSM

Official website http://www.nssm.cc/ , download address http://www.nssm.cc/download

One point two . build windows service

Find the install.bat created in the folder and click Run to open the operation window.

Install.bat content:


%~d0
cd %~dp0
nssm install NPSWebCoreService
PAUSE

Path: select the system to run bat —- C: \ NPs \ coreweb \ 1run.bat

PS: because special processing is needed in our system, bat is executed directly. Normally, fill in C: \ program files \ dotnet\ dotnet.exe . because I want to execute the dotnet command, these operations are actually executed in bat.

Startup directory: the directory of bat does not need to be changed;

PS: address of the publishing project folder.

Arguments: empty;

PS: project run DLL:

Service Name: I wrote it in the batch processing of install.bat. Don’t change it, because I’m dead when I restart the service in the code..

PS: just create a conduit name, that is, a Windows service name.

Then click the install Service button to complete the deployment.

Note: red font is the general processing mode, and non red font is the processing mode in our system

1.2.3 Start service

Let’s enter the website to visit

3. Nssm

3.1. Enter NSSM for its command line

3.2. Official website: http://www.nssm.cc/usage

4. Solutions to problems with our system

Four point one . log problem. Do not run bat when running path file in NSSM dotnet.exe . log4net configuration problem.

4.2. PDF problem of office conversion. Windows Servers 2008 server lacks COM component and windows. I added relevant code in the program. If you do not have permission, please create it manually (servers 2008 R2, 2016 can be created)

System.runtime.interopservices.comexception (0x800A03EC): Microsoft Office Excel cannot access the file “D: \ Benz \ Daimler \ Benz. Win \ lshbizplanfigure. XLS”. Possible causes are:

? file name or path does not exist.
? the file is being used by another program.
? the workbook you are saving has the same name as the currently open workbook.

I am very puzzled about this problem.

The solution is also very confusing

This solution is …

?Windows 2008 Server x64
Please make this folder.

C:\Windows\SysWOW64\config\systemprofile\Desktop

?Windows 2008 Server x86

Please make this folder.

C:\Windows\System32\config\systemprofile\Desktop

…instead of dcomcnfg.exe.

This operation took away office automation problems in my system.

A Desktop folder seems to be necessary in the systemprofile folder to open file by Excel.

It disappears from Windows2008, Windows2003 had the folder,
and I think it cause this error.

That is to say, the problem can be solved by creating a directory named desktop under the directories of C: \ windows \ system32 \ config \ systemprofile and C: \ windows \ syswow64 \ config \ systemprofile

It does solve my problem

4.3. The problem of database backup restart service is changed to restart windows service, so please use install.bat above to run do not change service name.

Restart service code:


try
 {
 ServiceController service = new ServiceController("NPSWebCoreService");
 if (service.Status == ServiceControllerStatus.Running)
 {
 service.Stop();
 service.WaitForStatus(ServiceControllerStatus.Stopped);
 }
 service.Start();
 service.WaitForStatus(ServiceControllerStatus.Running);
 }
 catch (Exception){}

4.4. NETCORE web should be based on the MySQL service problem. It is worried about the starting sequence of MySQL and windows services when the server is restarted. How do I judge whether the MySQL service is started when I start bat.

Bat content:

@echo off

SETLOCAL enabledelayedexpansion
REM turn off auto output
:begin

for /f "skip=3 tokens=4" %%i in ('sc query MySQL') do set "zt=%%i" &goto :next
:next
if /i "%zt%"=="RUNNING" (
 Echo has found that the service is running.
) else (
 net start MySQL80
 Echo has found that the service is running 1.
)

REM receive input
Input information from REM output
Echo starts npswebcore

dotnet ./NPSWebCore.dll

@End of echo startup
exit

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer.