1.安裝Nuget包
安裝以下nuget包支持windows service
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="6.0.10" /> <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.1" />
2.設定ContentRootPath
不設定ContentRootPath會指向C盤sc.exe所在檔案夾,
var options = new WebApplicationOptions { Args = args, ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default }; var builder = WebApplication.CreateBuilder(options);
3.啟用WIndowsService
通過以下方法即可啟用WIndowsService支持
builder.Host.UseWindowsService();
4.發布專案
右鍵專案,進行發布,發布到檔案夾

5.簡單安裝
通過以下BASH命令,實作安裝/開始/停止/洗掉windows service
安裝
set "CDir=%~dp0" sc.exe create "MyService" start=auto binpath="%CDir%MyService.exe --environment Development"
啟動
sc.exe start "MyService"
停止
sc.exe stop "MyService"
洗掉
sc.exe delete "MyService"
6.安裝打包工具
安裝Inno Setup打包工具
安裝好后,進行漢語支持支持,打開Inno Setup安裝目錄,默認C:\Program Files (x86)\Inno Setup 6,
復制Default.isl,將其拷貝至C:\Program Files (x86)\Inno Setup 6\Languages檔案夾,并重命名為Chinese.isl
將以下內容覆寫到Chinese.isl
; *** Inno Setup version 5.5.0+ Chinese messages *** ; ; To download user-contributed translations of this file, go to: ; http://www.jrsoftware.org/files/istrans/ ; ; Note: When translating this text, do not add periods (.) to the end of ; messages that didn't have them already, because on those messages Inno ; Setup adds the periods automatically (appending a period would result in ; two periods being displayed). [LangOptions] ; The following three entries are very important. Be sure to read and? ; understand the '[LangOptions] section' topic in the help file. LanguageName=Chinese LanguageID=$0409 LanguageCodePage=0 ; If the language you are translating to requires special font faces or ; sizes, uncomment any of the following entries and change them accordingly. ;DialogFontName= ;DialogFontSize=8 ;WelcomeFontName=Verdana ;WelcomeFontSize=12 ;TitleFontName=Arial ;TitleFontSize=29 ;CopyrightFontName=Arial ;CopyrightFontSize=8 [Messages] ; *** Application titles SetupAppTitle=安裝 SetupWindowTitle=安裝 - %1 UninstallAppTitle=卸載 UninstallAppFullTitle=%1 卸載 ; *** Misc. common InformationTitle=安裝資訊 ConfirmTitle=提示 ErrorTitle=錯誤 ; *** SetupLdr messages SetupLdrStartupMessage=現在將安裝 %1,是否繼續? LdrCannotCreateTemp=不能創建臨時檔案,安裝中止! LdrCannotExecTemp=不能在臨時目錄中解壓檔案,安裝程序中止! ; *** Startup error messages LastErrorMessage=%1.%n%n 錯誤 %2: %3 SetupFileMissing=在安裝目錄中找不到檔案 %1 ,請更正該問題或者獲得一個新的檔案, SetupFileCorrupt=安裝檔案被占用,請獲得一個新的安裝檔案, SetupFileCorruptOrWrongVer=安裝檔案被占用, 或者安裝檔案的版本不對,請更正該問題或者獲得一個新檔案, NotOnThisPlatform=該程式不能在 %1 下運行, OnlyOnThisPlatform=該程式必須在 %1 下運行, OnlyOnTheseArchitectures=該程式只能在以下WINDOWS版本下運行:%n%n%1 MissingWOW64APIs=該版本需要64位的安裝程式,要更正該問題,請安裝 Service Pack %1, WinVersionTooLowError=該軟體需要 %1 版本號 %2 或者更高, WinVersionTooHighError=該軟體不能在 %1 版本號 %2 或者更高版本中安裝, AdminPrivilegesRequired=您在安裝該程式時必須是管理員權限, PowerUserPrivilegesRequired=您在安裝程式是必須是管理員權限或者高權限用戶權限, SetupAppRunningError=安裝程式檢測到 %1 正在運行,%n%n請關閉它的所有實體,然后單擊“確認”繼續安裝,或者單擊“放棄”退出,UninstallAppRunningError=卸載程式檢測到 %1 正在運行,%n%n請關閉它的所有實體,然后單擊“確認”繼續安裝,或者單擊“放棄”退出, ; *** Misc. errors ErrorCreatingDir=安裝程式不能創建目錄 "%1" ErrorTooManyFilesInDir=不能在目錄"%1" 下創建檔案,因為該目錄下有太多的檔案了, ; *** Setup common messages ExitSetupTitle=退出安裝 ExitSetupMessage=安裝沒有完成, 如果您現在退出,程式將不被安裝,%n%n您可以下次在完成安裝任務,%n%n退出安裝? AboutSetupMenuItem=關于安裝程式[&A]... AboutSetupTitle=關于安裝程式 AboutSetupMessage=%1 版本 %2%n%3%n%n%1 主頁:%n%4 AboutSetupNote= TranslatorNote= ; *** Buttons ButtonBack=< 回退[&B] ButtonNext=下一步[&N] > ButtonInstall=安裝[&I] ButtonOK=確認 ButtonCancel=放棄 ButtonYes=市[&Y] ButtonYesToAll=全部確認[&A] ButtonNo=否[&N] ButtonNoToAll=全部否認[&o] ButtonFinish=完成[&F] ButtonBrowse=瀏覽[&B]... ButtonWizardBrowse=瀏覽[&r]... ButtonNewFolder=新建檔案夾[&M] ; *** "Select Language" dialog messages SelectLanguageTitle=選擇安裝語言種類 SelectLanguageLabel=選擇該語言作為安裝語言: ; *** Common wizard text ClickNext=單擊“下一步”繼續,或者單擊“放棄”退出本安裝程式, BeveledLabel= BrowseDialogTitle=瀏覽檔案夾 BrowseDialogLabel=在下面的串列中選擇一個目錄, 然后單擊“確認”繼續, NewFolderName=新檔案夾 ; *** "Welcome" wizard page WelcomeLabel1=歡迎進入 [name] 安裝向導 WelcomeLabel2=將在您的電腦上安裝 [name/ver] ,%n%n建議在繼續安裝之前退出其它程式的運行, ; *** "Password" wizard page WizardPassword=密碼 PasswordLabel1=本安裝程式受密碼保護, PasswordLabel3=請輸入密碼,密碼區分大小寫,然后單擊“下一步”繼續, PasswordEditLabel=密碼[&P]: IncorrectPassword=輸入的密碼不正確,請重新輸入, ; *** "License Agreement" wizard page WizardLicense=用戶許可協議 LicenseLabel=請在繼續之前仔細閱讀用戶許可協議, LicenseLabel3=請仔細閱讀下面的用戶許可協議,在繼續安裝之前,您必須接受該協議, LicenseAccepted=我接受協議[&a], LicenseNotAccepted=我不接受協議[&d] ; *** "Information" wizard pages WizardInfoBefore=提示 InfoBeforeLabel=在繼續安裝之前請仔細閱讀以下重要的資訊, InfoBeforeClickLabel=當您準備好繼續安裝時,單擊“繼續”, WizardInfoAfter=資訊 InfoAfterLabel=在繼續安裝之前請仔細閱讀以下重要的資訊, InfoAfterClickLabel=當您準備好繼續安裝時,單擊“繼續”, ; *** "User Information" wizard page WizardUserInfo=用戶資訊 UserInfoDesc=請輸入您的資訊, UserInfoName=用戶名[&U]: UserInfoOrg=組織[&O]: UserInfoSerial=序列號[&S]: UserInfoNameRequired=您必須輸入一個名稱, ; *** "Select Destination Location" wizard page WizardSelectDir=選擇目標位置 SelectDirDesc=您將把[name]安裝在哪里? SelectDirLabel3=安裝程式將把[name]安裝在下面的的檔案夾中, SelectDirBrowseLabel=為了繼續安裝, 請單擊“下一步”,如果您想選擇一個不同的目錄,請單擊“瀏覽”, DiskSpaceMBLabel=為了安裝本軟體,至少需要 [mb] MB 的空閑磁盤空間, ToUNCPathname=安裝程式不能安裝到一個 UNC 目錄名稱,如果您試圖網上安裝本程式,請映射網路驅動器, InvalidPath=您比如輸入一個含盤符的路徑名稱,比如:%n%nC:\APP%n%n 或者一個網路路徑,例如:%n%n\\server\share InvalidDrive=您選擇的磁盤或者網路路徑不存在或者不能訪問,請重新選擇其它的磁盤或者網路路徑, DiskSpaceWarningTitle=磁盤空間不足, DiskSpaceWarning=安裝程式至少需要 %1 KB 空閑磁盤空間來安裝本軟體,但是選擇的磁盤上只有 %2 KB 空間可用,%n%n 您確認繼續? DirNameTooLong=該檔案夾的名稱太長, InvalidDirName=該檔案夾的名字太長, BadDirName32=檔案夾名稱中不能包括以下的任何字符:%n%n%1 DirExistsTitle=檔案夾已經存在 DirExists=檔案夾:%n%n%1%n%n已經存在,您想繼續安裝? DirDoesntExistTitle=檔案夾不存在 DirDoesntExist=檔案夾:%n%n%1%n%n不存在,您想創建該檔案夾? ; *** "Select Components" wizard page WizardSelectComponents=選擇部件 SelectComponentsDesc=哪些部件需要安裝? SelectComponentsLabel2=選擇您想要安裝的部件; 清除您不想安裝的部件,當您準備好后,請單擊“下一步” , FullInstallation=完全安裝 ; if possible don't translate 'Compact' as 'Minimal' (I mean 'Minimal' in your language) CompactInstallation=精簡安裝 CustomInstallation=自定義安裝 NoUninstallWarningTitle=部件存在 NoUninstallWarning=安裝程式檢測到以下部件已經在您的電腦中被安裝了:%n%n%1%n%n不選擇它們將不在您的電腦中安裝它們,%n%n您想就這樣繼續嗎? ComponentSize1=%1 KB ComponentSize2=%1 MB ComponentsDiskSpaceMBLabel=當前選擇項至少需要 [mb] MB 的空閑磁盤空間才能安裝, ; *** "Select Additional Tasks" wizard page WizardSelectTasks=選擇一個附加任務 SelectTasksDesc=您想選擇哪個附加任務? SelectTasksLabel2=選擇在安裝[name]時執行的附加任務, 然后單擊“下一步”, ; *** "Select Start Menu Folder" wizard page WizardSelectProgramGroup=選擇開始選單檔案夾 SelectStartMenuFolderDesc=您想在哪里放置軟體的快捷方式? SelectStartMenuFolderLabel3=安裝程式將在下面的檔案夾中創建軟體的快捷方式, SelectStartMenuFolderBrowseLabel=為了繼續,單擊“下一步”,如果您想另外選擇一個檔案夾,單擊“瀏覽”, MustEnterGroupName=您必須輸入一個檔案夾名稱, GroupNameTooLong=檔案夾名稱或者路徑名稱太長, InvalidGroupName=檔案夾名稱非法, BadGroupName=檔案夾名字中不能包含下面的字符:%n%n%1 NoProgramGroupCheck2=不創建開始選單中的檔案夾[&D] ; *** "Ready to Install" wizard page WizardReady=準備開始安裝 ReadyLabel1=安裝程式開始在您的電腦中安裝[name]. ReadyLabel2a=單擊“安裝”開始安裝本軟體,或者單擊“回退”修改安裝設定, ReadyLabel2b=單擊“安裝”開始安裝本軟體, ReadyMemoUserInfo=用戶資訊: ReadyMemoDir=安裝目標位置: ReadyMemoType=安裝種類: ReadyMemoComponents=選中的部件: ReadyMemoGroup=開始選單檔案夾: ReadyMemoTasks=附加任務: ; *** "Preparing to Install" wizard page WizardPreparing=準備安裝 PreparingDesc=安裝程式準備在您的電腦中安裝[name], PreviousInstallNotCompleted=安裝/卸載一個以前的程式沒有完成,您需要重新啟動您的電腦來完成安裝作業,%n%n當重新啟動您的電腦后,請運行安裝程式來完成安裝[name], CannotContinue=安裝程式不能繼續執行,請單擊“放棄”退出, ; *** "Installing" wizard page WizardInstalling=安裝中 InstallingLabel=安裝程式正在安裝[name],請等待, ; *** "Setup Completed" wizard page FinishedHeadingLabel=[name]安裝完成 FinishedLabelNoIcons=安裝程式已經在您的電腦中安裝了[name], FinishedLabel=安裝程式已經在您的電腦中安裝了[name],要執行本軟體,請單擊安裝好的本軟體圖示 , ClickFinish=單擊“完成”退出本安裝程式. FinishedRestartLabel=為了完成[name]的安裝, 安裝程式必須重新啟動您的電腦,您想現在就重新啟動? FinishedRestartMessage=為了完成[name]的安裝, 安裝程式必須重新啟動您的電腦,%n%您想現在就重新啟動? ShowReadmeCheck=是的,我想查看 README 檔案 YesRadio=是的,我想重新啟動計算機[&Y] NoRadio=不,我將稍后重新啟動計算機[&N] ; used for example as 'Run MyProg.exe' RunEntryExec=運行 %1 ; used for example as 'View Readme.txt' RunEntryShellExec=查看 %1 ; *** "Setup Needs the Next Disk" stuff ChangeDiskTitle=安裝程式需要下一個安裝盤 SelectDiskLabel2=請插入安裝盤 %1 并且單擊“確認”,%n%n如果該盤不是下面顯示的盤,請輸入正確的路徑或者單擊“瀏覽”, PathLabel=目錄[&P]: FileNotInDir2=在 "%2" 中沒有發現檔案,請插入正確的磁盤或者選擇其它的檔案夾, SelectDirectoryLabel=請輸入下一個磁盤中的正確位置, ; *** Installation phase messages SetupAborted=安裝沒有完成,%n%n請修復錯誤并重新安裝, EntryAbortRetryIgnore=單擊“重試”重新嘗試,單擊“忽略”將繼續安裝,或者單擊“放棄”退出安裝, ; *** Installation status messages StatusCreateDirs=創建目錄中... StatusExtractFiles=解壓檔案中... StatusCreateIcons=創建快捷方式中... StatusCreateIniEntries=創建INI單元中... StatusCreateRegistryEntries=創建注冊表內容中... StatusRegisterFiles=注冊檔案中... StatusSavingUninstall=保存卸載資訊中... StatusRunProgram=正在完成安裝... StatusRollback=恢復原來修改的內容中... ; *** Misc. errors ErrorInternal2=內部錯誤: %1 ErrorFunctionFailedNoCode=%1 失敗 ErrorFunctionFailed=%1 失敗; 代碼 %2 ErrorFunctionFailedWithMessage=%1 錯誤; 代碼 %2.%n%3 ErrorExecutingProgram=不能執行檔案:%n%1 ; *** Registry errors ErrorRegOpenKey=在打開注冊表鍵時發生錯誤:%n%1\%2 ErrorRegCreateKey=在創建注冊表鍵時發生錯誤:%n%1\%2 ErrorRegWriteKey=在些注冊表鍵時發生錯誤:%n%1\%2 ; *** INI errors ErrorIniEntry=在創建INI檔案時發生錯誤 "%1". ; *** File copying errors FileAbortRetryIgnore=單擊“重試”再試一次,單擊“忽略”忽略該檔案,或者單擊“放棄”退出安裝程式, FileAbortRetryIgnore2=單擊“重試”再試一次,單擊“忽略”忽略該檔案繼續安裝,或者單擊“放棄”退出安裝程式, SourceIsCorrupted=源檔案被使用 SourceDoesntExist=源檔案 "%1" 不存在 ExistingFileReadOnly=該已經存在的檔案是只讀屬性,%n%n單擊“重試”洗掉只讀屬性并重新嘗試,單擊“忽略”忽略該檔案,或者單擊“放棄”退出安裝程式, ErrorReadingExistingDest=當試圖讀取一個已經存在的檔案時發生了錯誤: FileExists=該檔案已經存在,%n%n您想覆寫它嗎? ExistingFileNewer=已經存在的檔案比安裝程式試圖安裝的檔案要新,建議您保留該檔案,%n%n您想保留已經存在的檔案嗎? ErrorChangingAttr=當試圖改變一個存在的檔案的屬性時發生了錯誤: ErrorCreatingTemp=當試圖在目標目錄中創建一個檔案時發生了錯誤: ErrorReadingSource=當試圖讀取一個檔案時發生了錯誤: ErrorCopying=當試圖復制一個檔案時發生了錯誤: ErrorReplacingExistingFile=當試圖覆寫已經存在的檔案時發生錯誤: ErrorRestartReplace=重新啟動置換失敗: ErrorRenamingTemp=當在目標目錄中重命名檔案時發生錯誤: ErrorRegisterServer=不能注冊 DLL/OCX: %1 ErrorRegisterServerMissingExport=沒有Dll注冊服務庫輸出 ErrorRegisterTypeLib=不能注冊以下型別庫: %1 ; *** Post-installation errors ErrorOpeningReadme=當打開 README 檔案時發生錯誤, ErrorRestartingComputer=安裝程式不能重新啟動電腦,請手動啟動, ; *** Uninstaller messages UninstallNotFound=檔案 "%1" 不存在,不能卸載, UninstallOpenError=檔案 "%1" 不能打開,不能卸載, UninstallUnsupportedVer=卸載記錄檔案 "%1" 不是基于本安裝程式的版本,不能完成軟體的洗掉作業, UninstallUnknownEntry=一個未知的物體 (%1) 存在于卸載記錄檔案中, ConfirmUninstall=您確定完全洗掉 %1 和所有基于它上面的部件嗎? UninstallOnlyOnWin64=該安裝程式只能在64位的Windows上執行卸載作業, OnlyAdminCanUninstall=該安裝程式只能在您具有管理員權限時才能執行卸載作業, UninstallStatusLabel= %1 正在被洗掉,請稍等, UninstalledAll=%1 被成功地從您的電腦中洗掉, UninstalledMost=%1 洗掉完成,%n%n有某些部件不能被洗掉,您可能需要手動洗掉它們, UninstalledAndNeedsRestart=為了完成 %1 的洗掉作業, 您必須重新啟動電腦,%n%n您想現在就重新啟動嗎? UninstallDataCorrupted=檔案"%1" 被占用,不能完成洗掉作業, ; *** Uninstallation phase messages ConfirmDeleteSharedFileTitle=洗掉共享程式檔案? ConfirmDeleteSharedFile2=系統指出以下的共享程式檔案將不再被使用,您是否想洗掉這些共享檔案?%n%n如果這些檔案洗掉后,其他程式仍然要使用它,可能其它程式的功能將受影響,如果您不能肯定,請選擇“否”,讓這些檔案保留在系統中不會對系統造成損害, SharedFileNameLabel=檔案名: SharedFileLocationLabel=位置: WizardUninstalling=卸載 StatusUninstalling=卸載 %1 中... ; The custom messages below aren't used by Setup itself, but if you make ; use of them in your scripts, you'll want to translate them. [CustomMessages] NameAndVersion=%1 版本 %2 AdditionalIcons=附加圖示: CreateDesktopIcon=創建桌面圖示[&d] CreateQuickLaunchIcon=創建快速啟動圖示[&Q] ProgramOnTheWeb=%1 on the Web UninstallProgram=卸載 %1 LaunchProgram=執行 %1 AssocFileExtension=用檔案擴展名 %2 匹配[&A] %1? AssocingFileExtension=正在用檔案擴展名 %2 匹配[&A] %1 中...
7.打包
打開Inno Setup,通過右上角File->New新建Inno Setup打包檔案,設定應用程式資訊

設定安裝目錄為前面的程序發布目錄

添加卸載快捷鍵

設定licence資訊

選擇安裝模式

選擇漢化語言

設定輸出目錄

腳本中添加安裝命令,卸載命令
[Run] Filename: "{sys}\sc.exe"; Parameters: "stop MyService" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "delete MyService" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "create MyService start=auto binPath=""{app}\MyWeb.exe""" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "start MyService" ; Flags: runhidden [UninstallRun] Filename: "{sys}\sc.exe"; Parameters: "stop MyService" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "delete MyService" ; Flags: runhidden
腳本最終內容
; Script generated by the Inno Setup Script Wizard. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "MyService" #define MyAppVersion "1.0.0" #define MyAppPublisher "My Company, Inc." #define MyAppURL "https://www.example.com/" [Setup] ; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) AppId={{0C9603FD-2587-41FD-A532-CB5294E7B56E} AppName={#MyAppName} AppVersion={#MyAppVersion} ;AppVerName={#MyAppName} {#MyAppVersion} AppPublisher={#MyAppPublisher} AppPublisherURL={#MyAppURL} AppSupportURL={#MyAppURL} AppUpdatesURL={#MyAppURL} DefaultDirName={autopf}\{#MyAppName} DefaultGroupName={#MyAppName} DisableProgramGroupPage=yes LicenseFile=D:\Mylicence.txt InfoBeforeFile=D:\Myshowafter.txt InfoAfterFile=D:\Myshowbefore.txt ; Uncomment the following line to run in non administrative install mode (install for current user only.) ;PrivilegesRequired=lowest OutputDir=C:\Users\c\Desktop OutputBaseFilename=mysetup Compression=lzma SolidCompression=yes WizardStyle=modern [Languages] Name: "chinese"; MessagesFile: "compiler:Languages\Chinese.isl" [Files] Source: "D:\MyWeb\bin\Release\net6.0\publish\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}" [Run] Filename: "{sys}\sc.exe"; Parameters: "stop MyService" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "delete MyService" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "create MyService start=auto binPath=""{app}\MyWeb.exe""" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "start MyService" ; Flags: runhidden [UninstallRun] Filename: "{sys}\sc.exe"; Parameters: "stop MyService" ; Flags: runhidden Filename: "{sys}\sc.exe"; Parameters: "delete MyService" ; Flags: runhidden
8.打包編譯
點擊編譯按鈕,打包好后,雙擊exe安裝

9.檢驗安裝
首先去windows 服務查看是否安裝成功

然后訪問服務

至此打包安裝完成,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/522840.html
標籤:.NET技术
