crm.core/code/Crm.Core.WebApi/Program.cs

106 lines
3.9 KiB
C#

using Crm.Core.WebApi.Middlewares;
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<string>("Exceptionless:ApiKey"), builder.Configuration.GetValue<string>("Exceptionless:ServerUrl"), new string[] { "crm-core-webapi" })
.CreateLogger();
Log.Logger = logger;
builder.Services.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddSerilog(logger);
});
// Add services to the container.
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddDGEntityFrameworkOracle<CrmDbContext>("appid");
builder.Services.AddDGEntityFramework<WeworkDbContext>(options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("crmContext"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("crmContext")));
});
builder.Services.AddExceptionless(builder.Configuration);
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.AddSignalR();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddSingleton<NotificationHub>();
builder.Services.AddCors(option =>
{
option.AddPolicy(MyAllowSpecificOrigins,
policy =>
{
policy.SetIsOriginAllowed(_ => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddHostedService<NotificationWorker>();
builder.Services.AddHostedService<ExecuteWorker>();
builder.Services.AddHostedService<ExecuteStatisticsWorker>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = "CRM CORE API",
Description = "CRM CORE 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.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.UseExceptionless();
app.UseAuthorization();
app.UseMiddleware<RequestLogContextMiddleware>();
app.UseDGEntityFrameworkOracle("crm_tg_dng8");
app.MapHub<NotificationHub>("/hub");
app.MapControllers();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
ex.ToExceptionless().Submit();
}
finally
{
Log.CloseAndFlush();
}