校园网上超市网站建设推广,广州自助公司建网站企业,wordpress搭建软件下载,经典案例网站这是一文说通系列的第二篇#xff0c;里面有些内容会用到第一篇中间件的部分概念。如果需要#xff0c;可以参看第一篇#xff1a;一文说通Dotnet Core的中间件一、前言后台任务在一些特殊的应用场合#xff0c;有相当的需求。比方#xff0c;我们需要实现一个定时任务、或… 这是一文说通系列的第二篇里面有些内容会用到第一篇中间件的部分概念。如果需要可以参看第一篇一文说通Dotnet Core的中间件 一、前言后台任务在一些特殊的应用场合有相当的需求。比方我们需要实现一个定时任务、或周期性的任务、或非API输出的业务响应、或不允许并发的业务处理像提现、支付回调等都需要用到后台任务。 通常我们在实现后台任务时有两种选择WebAPI和Console。下面我们会用实际的代码来理清这两种工程模式下后台任务的开发方式。二、开发环境基础工程这个Demo的开发环境是Mac VS Code Dotnet Core 3.1.2。$ dotnet --info
.NET Core SDK (reflecting any global.json):Version: 3.1.201Commit: b1768b4ae7Runtime Environment:OS Name: Mac OS XOS Version: 10.15OS Platform: DarwinRID: osx.10.15-x64Base Path: /usr/local/share/dotnet/sdk/3.1.201/Host (useful for support):Version: 3.1.3Commit: 4a9f85e9f8.NET Core SDKs installed:3.1.201 [/usr/local/share/dotnet/sdk].NET Core runtimes installed:Microsoft.AspNetCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]Microsoft.NETCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]首先在这个环境下建立工程创建Solution% dotnet new sln -o demo
The template Solution File was created successfully.
这次我们用Webapi创建工程% cd demo
% dotnet new webapi -o webapidemo
The template ASP.NET Core Web API was created successfully.Processing post-creation actions...
Running dotnet restore on webapidemo/webapidemo.csproj...Restore completed in 179.13 ms for demo/demo.csproj.Restore succeeded.
% dotnet new console -o consoledemo
The template Console Application was created successfully.Processing post-creation actions...
Running dotnet restore on consoledemo/consoledemo.csproj...Determining projects to restore...Restored consoledemo/consoledemo.csproj (in 143 ms).Restore succeeded.
把工程加到Solution中% dotnet sln add webapidemo/webapidemo.csproj
% dotnet sln add consoledemo/consoledemo.csproj
基础工程搭建完成。三、在WebAPI下实现一个后台任务WebAPI下后台任务需要作为托管服务来实现而托管服务需要实现IHostedService接口。 首先我们需要引入一个库% cd webapidemo
% dotnet add package Microsoft.Extensions.Hosting
引入后我们就有了IHostedService。 下面我们来做一个IHostedService的派生托管类namespace webapidemo
{public class DemoService : IHostedService{public DemoService(){}public Task StartAsync(CancellationToken cancellationToken){throw new NotImplementedException();}public Task StopAsync(CancellationToken cancellationToken){throw new NotImplementedException();}}
}
IHostedService需要实现两个方法StartAsync和StopAsync。其中StartAsync用于启动后台任务StopAsync主机Host正常关闭时触发。 如果派生类中有任何非托管资源那还可以引入IDisposable并通过实现Dispose来清理非托管资源。 这个类生成后我们将这个类注入到ConfigureServices中以使这个类在Startup.Configure调用之前被调用public void ConfigureServices(IServiceCollection services)
{services.AddControllers();services.AddHostedServiceDemoService();
}
下面我们用一个定时器的后台任务来加深理解namespace webapidemo
{public class TimerService : IHostedService, IDisposable{/* 下面这两个参数是演示需要非必须 */private readonly ILogger _logger;private int executionCount 0;/* 这个是定时器 */private Timer _timer;public TimerService(ILoggerTimerService logger){_logger logger;}public void Dispose(){_timer?.Dispose();}private void DoWork(object state){var count Interlocked.Increment(ref executionCount);_logger.LogInformation($Service proccessing {count});}public Task StartAsync(CancellationToken cancellationToken){_logger.LogInformation(Service starting);_timer new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));return Task.CompletedTask;}public Task StopAsync(CancellationToken cancellationToken){_logger.LogInformation(Service stopping);_timer?.Change(Timeout.Infinite, 0);return Task.CompletedTask;}}
}
注入到ConfigureServices中public void ConfigureServices(IServiceCollection services)
{services.AddControllers();services.AddHostedServiceTimerService();
}
就OK了。代码比较简单就不解释了。四、WebAPI后台任务的依赖注入变形上一节的示例是一个简单的形态。下面我们按照标准的依赖注入实现一下这个定时器。 依赖注入的简单样式请参见一文说通Dotnet Core的中间件。 首先我们创建一个接口IWorkServicenamespace webapidemo
{public interface IWorkService{Task DoWork();}
}
再根据IWorkService建立一个实体类namespace webapidemo
{public class WorkService : IWorkService{private readonly ILogger _logger;private Timer _timer;private int executionCount 0;public WorkService(ILoggerWorkService logger){_logger logger;}public async Task DoWork(){var count Interlocked.Increment(ref executionCount);_logger.LogInformation($Service proccessing {count});}}
}
这样就建好了依赖的全部内容。 下面创建托管类namespace webapidemo
{public class HostedService : IHostedService, IDisposable{private readonly ILoggerHostedService _logger;public IServiceProvider Services { get; }private Timer _timer;public HostedService(IServiceProvider services, ILoggerHostedService logger){Services services;_logger logger;}public void Dispose(){_timer?.Dispose();}private void DoWork(object state){_logger.LogInformation(Service working);using (var scope Services.CreateScope()){var scopedProcessingService scope.ServiceProvider.GetRequiredServiceIWorkService();scopedProcessingService.DoWork().GetAwaiter().GetResult();}}public Task StartAsync(CancellationToken cancellationToken){_logger.LogInformation(Service starting);_timer new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));return Task.CompletedTask;}public Task StopAsync(CancellationToken cancellationToken){_logger.LogInformation(Service stopping);_timer?.Change(Timeout.Infinite, 0);return Task.CompletedTask;}}
}
把托管类注入到ConfigureServices中public void ConfigureServices(IServiceCollection services)
{services.AddControllers();services.AddHostedServiceHostedService();services.AddSingletonIWorkService, WorkService();
}
这样就完成了。 这种模式下可以根据注入的内容切换应用的执行内容。不过这种模式需要注意services.AddSingleton、services.AddScoped和services.AddTransient的区别。五、Console下的后台任务Console应用本身就是后台运行所以区别于WebAPI它不需要托管运行也不需要Microsoft.Extensions.Hosting库。我们要做的就是让程序运行就OK。 下面是一个简单的Console模板namespace consoledemo
{class Program{private static AutoResetEvent _exitEvent;static async Task Main(string[] args){/* 确保程序只有一个实例在运行 */bool isRuned;Mutex mutex new Mutex(true, OnlyRunOneInstance, out isRuned);if (!isRuned)return;await DoWork();/* 后台等待 */_exitEvent new AutoResetEvent(false);_exitEvent.WaitOne();}private static async Task DoWork(){throw new NotImplementedException();}}
}
这个模板有两个关键的内容单实例运行通常后台任务只需要有一个实例运行。所以第一个小段是解决单实例运行的。多次启动时除了第一个实例外其它的实例会自动退出后台等待看过很多人写的在这儿做后台等待时用了一个无限的循环。类似于下面的while(true)
{Thread.Sleep(1000);
}
这种方式也没什么太大的问题。不过这段代码总是要消耗CPU的计算量虽然很少但做为后台任务或者说Service毕竟是一种消耗而且看着不够高大上。 当然如果我们需要中断我们也可以把这个模板改成这样namespace consoledemo
{class Program{private static AutoResetEvent _exitEvent;static async Task Main(string[] args){bool isRuned;Mutex mutex new Mutex(true, OnlyRunOneInstance, out isRuned);if (!isRuned)return;_exitEvent new AutoResetEvent(false);await DoWork(_exitEvent);_exitEvent.WaitOne();}private static async Task DoWork(AutoResetEvent _exitEvent){/* Your Code Here */_exitEvent.Set();}}
}
这样就可以根据需要来实现中断程序并退出。六、Console应用的其它运行方式上一节介绍的Console其实是一个应用程序。在实际应用中Console程序跑在Linux服务器上我们可能会有一些其它的要求定时运行Linux上有一个Service叫cron是一个用来定时执行程序的服务。这个服务的设定需要另一个命令crontab位置在/usr/bin下。具体命令格式这儿不做解释网上随便查。运行到后台命令后边加个字符即可$ ./command
运行为Service需要持续运行的应用如果以Console的形态存在则设置为Service是最好的方式。Linux下设置一个应用为Service很简单就这么简单三步第一步在/etc/systemd/system下面创建一个service文件例如command.service[Unit]
# Service的描述随便写
DescriptionCommand[Service]
RestartSec2s
Typesimple
# 执行应用的默认用户。应用如果没有特殊要求最好别用root运行
Useryour_user_name
Groupyour_group_name
# 应用的目录绝对路径
WorkingDirectoryyour_app_folder
# 应用的启动路径
ExecStartyour_app_folder/your_app
Restartalways[Install]
WantedBymulti-user.target
差不多就这么个格式。参数的详细说明可以去网上查实际除了设置就是运行了一个脚本。第二步把这个command.service加上运行权限# chmod x ./command.service
第三步注册为Service# systemctl enable command.service
完成。为了配合应用还需要记住两个命令启动和关闭Service# #启动Service
# systemctl start command.service
# #关闭Service
# systemctl stop command.service
七、写在后边的话今天这个文章是因为前两天一个兄弟跑过来问我关于数据总线的实现方式而想到的一个点。 很多时候大家在写代码的时候会有一种固有的思想写WebAPI就想在这个框架中把所有的内容都实现了。这其实不算是一个很好的想法。WebAPI在业务层面就应该只是实现简单的处理请求返回结果的工作而后台任务跟这个内容截然不同通常它只做处理不做返回 --- 事实上也不太好返回要么客户端等待时间太长要么客户端已经断掉了。换句话说用WebAPI实现总线绝不是一个好的方式。不过Console运行为Service倒是一个总线应用的绝好方式。如果需要按序执行可以配合MQ服务器例如RabbitMQ来实现消息的按序处理。 再说代码。很多需求本来可以用很简单的方式实现。模式这个东西用来面试用来讲课都是很好的内容但实际开发中如果有更简单更有效的方式用起来Coding的工作是实现而不是秀技术。当然能否找到简单有效的方式这个可能跟实际的技术面有关系。但这并不是一个不能跨越的坎。多看多想每天成长一点点 今天的代码在https://github.com/humornif/Demo-Code/tree/master/0012/demo全文完点「在看」让更多人因你而受益↘ ↘ ↘