98 lines
3.4 KiB
C#
98 lines
3.4 KiB
C#
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<string>("Exceptionless:ApiKey"), builder.Configuration.GetValue<string>("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<WeworkDbContext>(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<IHttpContextAccessor, HttpContextAccessor>();
|
|
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();
|
|
} |