You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Platform/Startup.cs

123 lines
4.6 KiB

using Hangfire;
using Hangfire.Dashboard;
using Hangfire.MySql;
using Hangfire.Storage.SQLite;
using Infrastructure.Email;
using Infrastructure.Extensions;
using Infrastructure.Sms;
using Infrastructure.UI;
using Infrastructure.Web;
using IoT.Shared.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Platform.Data;
using Platform.EventHandlers;
using Platform.Services;
using System;
using System.Collections.Generic;
using System.Transactions;
using UoN.ExpressiveAnnotations.NetCore.DependencyInjection;
namespace Platform
{
public class Startup : BaseStartup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env)
{
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddMiniProfiler(o => {
o.RouteBasePath = "/profiler";// /profiler/results-index /profiler/results
o.ResultsAuthorize = o => {
return Configuration.GetAppSetting("Profiler", false) == true;
};
}).AddEntityFramework();
services.AddTransient<SceneTiggerService>();
services.AddTransient<ISceneTiggerService, CachedSceneTiggerService>();
services.AddTransient<IExecApiService,ExecApiService>();
services.AddTransient<IUserService, UserService>();
if (Env.IsDevelopment())
{
services.AddTransient<IEmailSender, EmptyEmailSender>();
services.AddTransient<ISmsSender, EmptySmsSender>();
}
else
{
services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<ISmsSender, NetEasySmsSender>();
}
services.AddTransient<DataService>();
services.AddTransient<IoTCenterEventHandler>();//hangfire需要
services.AddHostedService<DataSyncService>();
base.ConfigureServices(services);
if (Configuration.GetAppSetting("JobDatabase") == "MySQL")
{
var connectionString = Configuration.GetConnectionString("Job.MySQL");
services.AddHangfire(configuration => configuration.UseStorage(new MySqlStorage(connectionString, new MySqlStorageOptions
{
TransactionIsolationLevel = IsolationLevel.ReadCommitted,
QueuePollInterval = TimeSpan.FromSeconds(15),
JobExpirationCheckInterval = TimeSpan.FromHours(1),
CountersAggregateInterval = TimeSpan.FromMinutes(5),
PrepareSchemaIfNecessary = true,
DashboardJobListLimit = 50000,
TransactionTimeout = TimeSpan.FromMinutes(1),
TablesPrefix = "Hangfire"
})));
}
else
{
services.AddHangfire(o => o.UseSQLiteStorage("job.db"));
}
services.AddReverseProxy().LoadFromConfig(Configuration.GetSection("ReverseProxy"));
services.AddExpressiveAnnotations();
}
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMiniProfiler();
base.Configure(app, env, loggerFactory);
var options = new DashboardOptions
{
Authorization = new[] { new MyDashboardAuthorizationFilter() },
IsReadOnlyFunc = o => o.GetHttpContext().User.Identity.Name != "super",
DashboardTitle="定时任务看板"
};
app.UseHangfireDashboard("/job", options);
app.UseHangfireServer(new BackgroundJobServerOptions
{
ServerName = $"IoTCenter:{Guid.NewGuid()}",
});
app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy();
});
}
public override void ConfigureOptions(IServiceCollection services)
{
//services.ConfigureOptions(new FileConfigureOptions(Env, new List<string> { "IoT.Shared" }));
}
public override void UseSignalR(IEndpointRouteBuilder endpoints)
{
this.UseSignalR<IoTCenterHub>(endpoints);
}
public override void AddDbContext(IServiceCollection services)
{
this.AddDbContext<PlatformDbContext>(services);
}
}
}