using Crm.Core.EntityFramework; using DG.Core; using DG.EntityFramework; using DG.EventBus; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Serilog; try { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("Serilog.json") .AddJsonFile($"Serilog.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true) .Build(); var builder = WebApplication.CreateBuilder(args); var logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .WriteTo.Exceptionless(builder.Configuration.GetValue("Exceptionless:ApiKey"), builder.Configuration.GetValue("Exceptionless:ServerUrl"), new string[] { "crm-identity-webapi" }) .CreateLogger(); Log.Logger = logger; builder.Services.AddLogging(logging => { logging.ClearProviders(); logging.AddSerilog(logger); }); // Add services to the container. var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; builder.Services.AddDGEntityFramework(options => { options.UseMySql(builder.Configuration.GetConnectionString("wework"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("wework"))); }); builder.Services.AddControllers() .AddApiResult() .AddJsonOptions(options => { //时间格式化响应 options.JsonSerializerOptions.Converters.Add(new JsonOptionsExtensions()); }); builder.Services .AddAutoIoc(typeof(IScopedDependency), LifeCycle.Scoped) .AddAutoIoc(typeof(ISingletonDependency), LifeCycle.Singleton) .AddAutoIoc(typeof(ITransientDependency), LifeCycle.Transient) .AddMapper(); builder.Services.AddEventBus(); builder.Services.AddSingleton(); builder.Services.AddCors(option => { option.AddPolicy(MyAllowSpecificOrigins, policy => { policy.SetIsOriginAllowed(_ => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Version = "v1", Title = "CRM Identity API", Description = "CRM Identity API" }); var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml"; options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Crm.Core.Identity.Domain.xml")); }); builder.Services.AddRedis(builder.Configuration); builder.Services.AddDGHttpClient(); var app = builder.Build(); app.UseCors(MyAllowSpecificOrigins); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment() || Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "PreProduction") { app.UseSwagger(); app.UseSwaggerUI(); } app.UseAuthorization(); app.MapControllers(); app.Run(); } catch (Exception ex) { Log.Fatal(ex, "Host terminated unexpectedly"); } finally { Log.CloseAndFlush(); }