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 服务查看是否安装成功
然后访问服务
至此打包安装完成。
总结
到此这篇关于.net6打包部署到windows service的文章就介绍到这了,更多相关.net6打包部署windows service内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论