使用Host编程模型编写Windows服务应用
来源:http://www.tudoupe.com时间:2022-03-18
以前,使用者只能通过这样做来准备在职服务申请,但现在他们可以直接这样做。我们试图在本章中使用的服务申请。
在完成这一课程后,你们将了解以下各点:
如何使模板应用程序可执行 。
创建一个服务
启动和停止服务
检查服务活动日志。
管理服务
创建项目
您可以设计一个基于模板的项目, 或者使用一个工具进行设计 。
必须制作的服务应用程序需要安装以下额外包件:
这个一揽子计划是为了准备一揽子服务,我们需要增加另一个一揽子计划,仅用于示范目的:
编写代码
下一步, 使用打开的文件夹, 创建一个新文件 : 然后用代码替换它 :
namespaceWindowsService;
publicclassJokeService
{
publicstringGetJoke()
{
Jokejoke=_jokes.ElementAt(
Random.Shared.Next(_jokes.Count));
return$"";
}
// Programming jokes borrowed from:
// https://github.com/eklavyadev/karljoke/blob/main/source/jokes.json
readonlyHashSet_jokes=new()
{
newJoke("What's the best thing about a Boolean?","Even if you're wrong, you're only off by a bit."),
newJoke("What's the object-oriented way to become wealthy?","Inheritance"),
newJoke("Why did the programmer quit their job?","Because they didn't get arrays."),
newJoke("Why do programmers always mix up Halloween and Christmas?","Because Oct 31 == Dec 25"),
newJoke("How many programmers does it take to change a lightbulb?","None that's a hardware problem"),
newJoke("If you put a million monkeys at a million keyboards, one of them will eventually write a Java program","the rest of them will write Perl"),
newJoke("['hip', 'hip']","(hip hip array)"),
newJoke("To understand what recursion is...","You must first understand what recursion is"),
newJoke("There are 10 types of people in this world...","Those who understand binary and those who don't"),
newJoke("Which song would an exception sing?","Can't catch me - Avicii"),
newJoke("Why do Java programmers wear glasses?","Because they don't C#"),
newJoke("How do you check if a webpage is HTML5?","Try it out on Internet Explorer"),
newJoke("A user interface is like a joke.","If you have to explain it then it is not that good."),
newJoke("I was gonna tell you a joke about UDP...","...but you might not get it."),
newJoke("The punchline often arrives before the set-up.","Do you know the problem with UDP jokes?"),
newJoke("Why do C# and Java developers keep breaking their keyboards?","Because they use a strongly typed language."),
newJoke("Knock-knock.","A race condition. Who is there?"),
newJoke("What's the best part about TCP jokes?","I get to keep telling them until you get them."),
newJoke("A programmer puts two glasses on their bedside table before going to sleep.","A full one, in case they gets thirsty, and an empty one, in case they don’t."),
newJoke("There are 10 kinds of people in this world.","Those who understand binary, those who don't, and those who weren't expecting a base 3 joke."),
newJoke("What did the router say to the doctor?","It hurts when IP."),
newJoke("An IPv6 packet is walking out of the house.","He goes nowhere."),
newJoke("3 SQL statements walk into a NoSQL bar. Soon, they walk out","They couldn't find a table.")
};
}
publicrecordJoke(stringSetup,stringPunchline);
to
重命名文件并插入以下代码:
namespaceApp.WindowsService;
publicsealedclassWindowsBackgroundService:BackgroundService
{
privatereadonlyJokeService_jokeService;
privatereadonlyILogger_logger;
publicWindowsBackgroundService(
JokeServicejokeService,
ILoggerlogger)=>
(_jokeService, _logger)=(jokeService, logger);
protectedoverrideasyncTaskExecuteAsync(CancellationTokenstoppingToken)
{
while(!stoppingToken.IsCancellationRequested)
{
stringjoke=_jokeService.GetJoke();
_logger.LogWarning(joke);
awaitTask.Delay(TimeSpan.FromMinutes(1), stoppingToken);
}
}
}
更改
替换文件中的以下文字:
usingWindowsService;
IHosthost=Host.CreateDefaultBuilder(args)
.UseWindowsService(
options=>
{
options.ServiceName=".Net Joke Service";
}
)
.ConfigureServices(services=>
{
services.AddSingleton();
services.AddHostedService();
})
.Build();
awaithost.RunAsync();
发布应用
如果服务得到发展,建议将以可执行文件的形式提出。
为此,应修改并在文件中替换以下内容:
下列命令可直接用于发出申请:
可以看出以下结果:
以下是获得文件的绝对路线:
创建服务
我们已经建立了直角路径, 所以我们将建立服务:
应当提及的是,另一个应作为管理人开放和运作。
如果服务界面在现阶段是活跃的,可以观察到以下服务:
您也可以处理:

观察日志
打开的事件查看器,

禁用 Windows 服务
在管理员窗口中执行以下命令 :
至此演示完成。
往期文章目录:
.Net 基础
Azure Developer是一个很好的讨论和学习Azure技术的地方,
上一篇:微软(MSFT.US)面临来自法国云计算公司的反垄断诉讼
下一篇:没有了
相关新闻
- 2022-03-18 微软(MSFT.US)面临来自法国云计算
- 2022-03-18 想要免费AI语言合成?这款微软官方
- 2022-03-18 微软宣布在一种全新类型的量子位
- 2022-03-18 微软在欧洲面临有关其云服务的反
- 2022-03-18 Windows实用效率工具分享!
- 2022-03-18 微软将在芬兰投建区域数据中心,
- 2022-03-18 统信软件正式官宣,微软也没料到
- 2022-03-18 Win10预览版本Build 19044.1618更新来啦
- 2022-03-18 微软将对基于HoloLens打造的军用AR设
- 2022-03-18 微软回馈玩家:玩免费游戏白送X
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
