using Furion;
|
using Furion.EventBus;
|
using MediatR;
|
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Hosting;
|
using Newtonsoft.Json.Converters;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace FlexJobApi.Core
|
{
|
[AppStartup(2)]
|
public class FlexJobApiCoreStartup : AppStartup
|
{
|
public void ConfigureServices(IServiceCollection services)
|
{
|
services.AddConsoleFormatter(options =>
|
{
|
options.WithTraceId = true;
|
options.WithStackFrame = true;
|
});
|
|
services.AddFileLogging(options =>
|
{
|
options.WithTraceId = true;
|
options.WithStackFrame = true;
|
options.FileNameRule = fileName =>
|
{
|
return string.Format(fileName, DateTime.UtcNow); // 如果是本地时间使用 DateTime.Now
|
};
|
});
|
|
services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
|
{
|
options.WithTraceId = true;
|
options.WithStackFrame = true;
|
});
|
|
services.AddComponent<EventBusServiceComponent>();
|
|
services.AddComponent<DistributedCacheServiceComponent>();
|
|
services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(App.Assemblies.ToArray()));
|
|
services.AddHostedService<XmlDocBuildHostedService>();
|
|
services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
|
|
services.AddCorsAccessor();
|
|
services.AddSpecificationDocuments(options =>
|
{
|
options.OperationFilter<FurionCustomOperationIdFilter>();
|
});
|
|
services.AddControllers()
|
.AddNewtonsoftJson(options =>
|
{
|
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
})
|
.AddFriendlyException()
|
.AddDataValidation()
|
.AddInjectWithUnifyResult<FriendlyResultProvider>();
|
}
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
{
|
// 解析事件总线发布服务
|
var eventPublisher = app.ApplicationServices.GetRequiredService<IEventPublisher>();
|
|
// 订阅
|
eventPublisher.OnExecuted += (sender, args) =>
|
{
|
Console.WriteLine($"事件 {args.Source.EventId} 执行状态:{args.Status},异常:{args.Exception},执行结果:{args.Result}");
|
};
|
|
if (env.IsDevelopment())
|
{
|
app.UseDeveloperExceptionPage();
|
}
|
|
app.UseUnifyResultStatusCodes();
|
|
app.UseHttpsRedirection();
|
|
app.UseRouting();
|
|
app.UseCorsAccessor();
|
|
app.UseAuthentication();
|
app.UseAuthorization();
|
|
app.UseInject(string.Empty);
|
|
app.UseSpecificationDocuments();
|
|
app.UseEndpoints(endpoints =>
|
{
|
endpoints.MapControllers();
|
});
|
}
|
}
|
}
|