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

94 lines
3.2 KiB
C#

try
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Serilog.json")
.AddJsonFile($"Serilog.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Log.Logger = logger;
var builder = WebApplication.CreateBuilder(args);
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.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.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddCors(option =>
{
option.AddPolicy(MyAllowSpecificOrigins,
policy =>
{
policy.SetIsOriginAllowed(_ => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
// 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 EXTERNAL API",
Description = "CRM EXTERNAL 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.External.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.UseRouting();
app.UseDGEntityFrameworkOracle("crm_tg_dng8");
app.MapControllers();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}