ClearEvents: CreateHostBuilder replcaed with WebApplicationBuilder

This commit is contained in:
Maksim Chegulov 2022-02-01 14:16:48 +03:00
parent 1b32b993b6
commit 7f74aa544c

View File

@ -24,89 +24,82 @@
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ASC.Api.Core;
using ASC.ClearEvents.Services;
using ASC.Common;
using ASC.Common.Caching;
using ASC.Common.DependencyInjection;
using ASC.Common.Utils;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using StackExchange.Redis.Extensions.Core.Configuration;
namespace ASC.Thumbnails.Svc
var options = new WebApplicationOptions
{
public class Program
{
public async static Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSystemd()
.UseWindowsService()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureAppConfiguration((hostContext, config) =>
{
var buided = config.Build();
var path = buided["pathToConf"];
if (!Path.IsPathRooted(path))
{
path = Path.GetFullPath(CrossPlatform.PathCombine(hostContext.HostingEnvironment.ContentRootPath, path));
}
config.SetBasePath(path);
var env = hostContext.Configuration.GetValue("ENVIRONMENT", "Production");
config
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddInMemoryCollection(new Dictionary<string, string>
{
{"pathToConf", path }
}
);
})
.ConfigureServices((hostContext, services) =>
{
services.AddMemoryCache();
var diHelper = new DIHelper(services);
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = hostContext.Configuration.GetSection("kafka").Get<KafkaSettings>();
var builder = WebApplication.CreateBuilder(options);
if (kafkaConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
}
else if (redisConfiguration != null)
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>));
}
else
{
diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
}
services.AddHostedService<ClearEventsServiceLauncher>();
diHelper.TryAdd<ClearEventsServiceLauncher>();
})
.ConfigureContainer<ContainerBuilder>((context, builder) =>
{
builder.Register(context.Configuration, false, false);
})
.ConfigureNLogLogging();
}
}
builder.Host.UseSystemd();
builder.Host.UseWindowsService();
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureAppConfiguration((hostContext, config) =>
{
var configRoot = config.Build();
var path = configRoot["pathToConf"];
if (!Path.IsPathRooted(path))
path = Path.GetFullPath(CrossPlatform.PathCombine(hostContext.HostingEnvironment.ContentRootPath, path));
config.SetBasePath(path);
var env = hostContext.Configuration.GetValue("ENVIRONMENT", "Production");
config.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddInMemoryCollection(new Dictionary<string, string> { { "pathToConf", path } });
});
builder.Host.ConfigureServices((hostContext, services) =>
{
services.AddMemoryCache();
var diHelper = new DIHelper(services);
var redisConfiguration = hostContext.Configuration.GetSection("Redis").Get<RedisConfiguration>();
var kafkaConfiguration = hostContext.Configuration.GetSection("kafka").Get<KafkaSettings>();
if (kafkaConfiguration != null) diHelper.TryAdd(typeof(ICacheNotify<>), typeof(KafkaCache<>));
else if (redisConfiguration != null) diHelper.TryAdd(typeof(ICacheNotify<>), typeof(RedisCache<>));
else diHelper.TryAdd(typeof(ICacheNotify<>), typeof(MemoryCacheNotify<>));
services.AddHostedService<TimedClearEventsService>();
diHelper.TryAdd<TimedClearEventsService>();
});
builder.Host.ConfigureContainer<ContainerBuilder>((context, builder) =>
builder.Register(context.Configuration, false, false));
builder.Host.ConfigureNLogLogging();
var app = builder.Build();
await app.RunAsync();