You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.1 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common;
using System.Reflection;
using System.IO;
using Hangfire;
using System.Net.Mime;
using AutoMapper;
using djy_AmsApi;
using djy.Service;
using Common.Extensions;
using djy.Service.DjyService;
using djy.IService.Djy;
namespace djy_AmsApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
///
/// </summary>
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().ConfigureApiBehaviorOptions(options=> {
options.InvalidModelStateResponseFactory = context =>
{var result = new BadRequestObjectResult(context.HttpContext);
return result;
};
});
services.AddHttpClient();
//读取配置信息
Configuration.Bind("WebConfig", sysOptionConfig.Webconfig);
sysOptionConfig._Configuration = Configuration;
services.AddAuthentication("Bearer").AddJwtBearer("Bearer",option=> {
option.Authority = sysOptionConfig.Webconfig.IdentServerUrl;//授权服务的URl地址
option.RequireHttpsMetadata = false;//是否使用了https
option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{ ValidateAudience = false };
IdentityModelEventSource.ShowPII = true;
});
//初始化数据库连接池
DbContext.DbBusInit();
////批量循环依赖注册
var ISList = Assembly.Load("djy.IService").GetTypes().Where(w => w.Name.EndsWith("Service") && w.Name.StartsWith("I"));
var Slist = Assembly.Load("djy.Service").GetTypes().Where(w => w.Name.EndsWith("Service"));
foreach (var Is in ISList)
{
var sname = Slist.FirstOrDefault(w => w.Name == Is.Name.Substring(1));
if (sname != null)
{
services.AddTransient(Is, sname);
}
}
//automapper
services.AddAutoMapper(typeof(AutoMapperConfig));
//任务配置
services.AddHangfire(config => {
config.UseRedisStorage(sysOptionConfig.Webconfig.Redis, new Hangfire.Redis.RedisStorageOptions { Prefix = "hf_"+sysOptionConfig.Webconfig.WebName,InvisibilityTimeout=TimeSpan.FromHours(1) });
});
services.AddHangfireServer();
//CAP 消息队列总线
if (sysOptionConfig.Webconfig.Rbmq_Host.IsNotNull())
{
services.AddCap(x =>
{
x.UseSqlServer(sysOptionConfig.Webconfig.Rbmq_Sqlhost);
x.UseRabbitMQ(options =>
{
var _host = sysOptionConfig.Webconfig.Rbmq_Host.Split(':');
options.HostName = _host[0];
if (_host.Length == 2)
{
options.Port = int.Parse(_host[1]);
}
options.UserName = sysOptionConfig.Webconfig.Rbmq_UserName;
options.Password = sysOptionConfig.Webconfig.Rbmq_Password;
});
x.UseDashboard();
});
}
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "djy_AmsApi", Version = "v1" });
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
var xmlPath = Path.Combine(basePath, "AppServer.xml");
});
//初始化IOC
IOC.container = services;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//swagger
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "djy_AmsApi v1");
// c.RoutePrefix = "apidoc";
});
}
app.UseMiddleware<MytMiddleware>();//管道
app.UseRouting();
DefaultFilesOptions defaultFiles = new DefaultFilesOptions();
defaultFiles.DefaultFileNames.Clear();
defaultFiles.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(defaultFiles);
app.UseStaticFiles();
app.UseCors(builder => {
builder.AllowAnyHeader();
builder.AllowAnyMethod();
//builder.WithOrigins("http://www.baidu.com");//跨域配置
builder.AllowAnyOrigin();
});
app.UseAuthentication();//认证
app.UseAuthorization();//授权
app.UseHangfireDashboard("/job", new DashboardOptions() { Authorization = new[] { new HangFireAuthorizeFilter() } });
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
//启动运行启动
//_Job(env);
}
/// <summary>
/// 启动任务请求
/// </summary>
private void _Job(IWebHostEnvironment env)
{
//初始化数据字典
var _itool = IOC.AddServer<IToolsService>();
// _itool.bindConfigDict(true, 5);
_itool.AutoJob();
}
}
}