66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using Autofac;
|
|
using Autofac.Extensions.DependencyInjection;
|
|
using System.Text;
|
|
using WcsMain;
|
|
using WcsMain.Common;
|
|
using WcsMain.StartAction;
|
|
|
|
Console.Title = "WCS设备控制系统";
|
|
Console.OutputEncoding = Encoding.UTF8;
|
|
ConsoleLog.DisbleQuickEditMode();
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// 根据运行环境加载参数
|
|
var env = builder.Environment;
|
|
LoadingRunningData.Loading(env.IsDevelopment()); // 加载系统运行信息,此步骤报错将会产生问题
|
|
|
|
// Add services to the container.
|
|
//builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
|
builder.Services.AddControllers(options =>
|
|
{
|
|
//options.Filters.Add<ApiExceptionFilterAttribute>();
|
|
}).AddJsonOptions(options =>
|
|
{
|
|
// 修改返回配置,返回原实体类数据
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
|
});
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
builder.Services.AddHostedService<HostService>();
|
|
// 添加跨域,允许任何人访问
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("any", policyBuilder =>
|
|
{
|
|
policyBuilder.WithOrigins("*").AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
builder.WebHost.UseUrls(CommonData.Settings.UseUrls ?? ["http://*:890"]); // 地址请在 appSettings.json 里面配置,这里是默认值
|
|
|
|
// 使用 autoFac 替换注入容器
|
|
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
|
|
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
|
|
{
|
|
builder.RegisterModule<AutofacModule>();
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseCors("any");
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|