zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#if !NET45
using Prow.Tasks;
using Microsoft.Extensions.Options;
using System;
 
namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 后台服务注入方法扩展类
    /// </summary>
    public static class TaskServiceCollectionExtensions
    {
        /// <summary>
        /// 增加后台任务服务到容器中 <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <param name="configure"></param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddTaskServices(this IServiceCollection services, Action<ITaskStorageBuilder>? configure = null)
        {
            // 创建服务
            services.AddSingleton<IStorage, NoneStorage>();
            services.AddSingleton<IConfigureOptions<TaskServicesOptions>, TaskServicesConfigureOptions<TaskServicesOptions>>();
            services.AddSingleton<IOptionsChangeTokenSource<TaskServicesOptions>, ConfigurationChangeTokenSource<TaskServicesOptions>>();
            services.AddHostedService<TaskServicesFactory>();
            configure?.Invoke(new TaskStorageBuilder(services));
            return services;
        }
    }
}
#endif