首页 > 系统相关 >NetCore windowsService 看门狗应用程序

NetCore windowsService 看门狗应用程序

时间:2023-10-23 12:05:37浏览次数:36  
标签:Task log NetCore windowsService args 看门狗 public new builder

public class DogService : BackgroundService
    {

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            return base.StartAsync(cancellationToken);
        }
        /// <summary>
        ///  每一秒执行一次
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                  StartService();
                  await Task.Delay(1000 * 60 , stoppingToken);
            }
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        { 
            return base.StopAsync(cancellationToken);
        }  

        private void StartService()
        {
            List<string> services = new List<string>() { "ChargRabbitService" };
            foreach (var item in services)
            {
                ServiceController service = new ServiceController(item);
                try
                {
                    if (service.Status != ServiceControllerStatus.Running)
                    {

                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);

                    }
                }
                catch (Exception ex)
                {
                    continue;
                }
            }  
        }
    }

main

static class Program
    {
        static  void Main(string[] args)
        {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.AddJsonFile("appsettings.json", true, true);
            var ConfigRoot = builder.Build();//根节点
            IServiceCollection Services = new ServiceCollection();
            
            Services.AddLogging(log => { log.AddConsole(); log.AddNLog(); log.SetMinimumLevel(LogLevel.Error); });  
            //  
            using (ServiceProvider provider = Services.BuildServiceProvider())
            {
                

            }
            CreateHostBuilder(args).Run();
        }

        public static IHost CreateHostBuilder(string[] args)
        {
            var builder = Host.CreateDefaultBuilder(args)
                 .ConfigureServices((hostContext, services) =>
                 {
                     services.AddHostedService<DogService>();
                 }).UseWindowsService();
            var host = builder.Build();

            return host;
        }
    }

将发布的exe 设置为 以管理员身份运行 【属性-安全】



标签:Task,log,NetCore,windowsService,args,看门狗,public,new,builder
From: https://blog.51cto.com/u_15316082/7985386

相关文章

  • NETCORE DockerFile
    1.DockerFile配置rm-rfDockerfilecat>>Dockerfile<<EOFFROMmcr.microsoft.com/dotnet/aspnet:3.1ASbase#RUNapkadd--no-cacheicu-libsEXPOSE80EXPOSE443#buildFROMmcr.microsoft.com/dotnet/sdk:3.1ASbuildRUNdotnetnugetaddsource......
  • .netframework迁移到.netcore方法
    一.netframework程序迁移到.netcore5.0对于.netframwork程序想要升级为.netcore5.0的方法,微软官方也给出了方法见https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-5.0,我这里总结记录一下.1.首先要检查自己应用程序适不适合迁移.netfr......
  • 嵌入式专题研究2:系统时钟和定时器和看门狗
    时钟系统:构成:1.振荡器——脉搏RC振荡器晶体振荡器四种:HSE,HSI,LSE,LSI2.唤醒定时器3.PLL倍频器4.各种分频器 可以通过stm32cubemx进行时钟配置,但是在SystemClock_Config()函数前,在启动文件中有默认的时钟配置。while(1){HAL_GPIO_WritePin......
  • NetCore Ocelot 之 Cache
    OcelotsupportssomeveryrudimentarycachingatthemomentproviderbytheCacheManagerproject.Thissanamazingprojectthatissolvingalotofcachingproblems.IwouldrecommendusingthispackagetocachewithOcelot.Thefollowingexampleshowsh......
  • NetCore Ocelot 之 Authorization
    Ocelotsupportsclaimsbasedauthorizationwhichisrunpostauthentication.ThismeansifouhavearouteyouwanttoauthorizeyoucanaddthefollowingtoyouRouteconfiguration."RouteClaimsRequirement":{"client_role":......
  • NetCore Ocelot 之 Qos
    QosqualityofserviceOcelotsupportsoneQoscapabilityatthecurrenttime.YoucansetonaperRoutebasisifyouwanttouseacircuitbreakerwhenmakingrequeststoadownstreamservice.Thisusesanawesome.NETlibrarycalledPolly.Thefirstthi......
  • NetCore Ocelot 之 Load Balancer
    OcelotcanloadbalanceacrossavailabledownstreamservicesforeachRoute.ThismeansyoucanscaleyourdownstreamservicesandOcelotcanusethemeffectively.TheTypeofloadbalanceravailbleare:  LeastConnection -trackswhichservicearedeal......
  • NetCore Ocelot 之 Authentication
    InordertoauthenticateRoutesandsubsequentlyuseanyofOcelot'sclaimsbasedfeaturessuchasauthorizationormodifyingtherequestwithvaluesfromthetoken.UsersmustregisterauthenticationservicesintheirStartup.csasusualbuttheypr......
  • NetCore Ocelot 之 Rate Limiting
    Ocelotsupportsratelimitingofupstreamrequestssothatyourdownstreamservicesdonotbecomeoverloaded.OKsotogetratelimitingworkingforaRouteyouneedtoaddthefollowingjsontoit."RateLimitOptions":{"ClientWhi......
  • NetCore学习笔记:单元测试和集成测试
    前言#我在使用AspNetCore的这段时间内,看了很多开源项目和博客,发现各种.Net体系的新技术很多人都有关注和使用,但却很少有人关注测试。测试是软件生命周期中的一个非常重要的阶段,对于保证软件的可靠性具有极其重要的意义。在应用程序的开发过程中,为了确保它的功能与预期一致,......