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#

3 years ago
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;
3 years ago
using Common.Extensions;
2 years ago
using djy.Service.DjyService;
2 years ago
using djy.IService.Djy;
3 years ago
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();
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
3 years ago
Configuration.Bind("WebConfig", sysOptionConfig.Webconfig);
3 years ago
sysOptionConfig._Configuration = Configuration;
services.AddAuthentication("Bearer").AddJwtBearer("Bearer",option=> {
3 years ago
option.Authority = sysOptionConfig.Webconfig.IdentServerUrl;//<2F><>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>URl<52><6C>ַ
3 years ago
option.RequireHttpsMetadata = false;//<2F>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>https
option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{ ValidateAudience = false };
IdentityModelEventSource.ShowPII = true;
});
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ӳ<EFBFBD>
DbContext.DbBusInit();
////<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
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));
3 years ago
3 years ago
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
services.AddHangfire(config => {
3 years ago
config.UseRedisStorage(sysOptionConfig.Webconfig.Redis, new Hangfire.Redis.RedisStorageOptions { Prefix = "hf_"+sysOptionConfig.Webconfig.WebName,InvisibilityTimeout=TimeSpan.FromHours(1) });
3 years ago
});
services.AddHangfireServer();
//CAP <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
3 years ago
if (sysOptionConfig.Webconfig.Rbmq_Host.IsNotNull())
3 years ago
{
services.AddCap(x =>
{
3 years ago
x.UseSqlServer(sysOptionConfig.Webconfig.Rbmq_Sqlhost);
3 years ago
x.UseRabbitMQ(options =>
{
3 years ago
var _host = sysOptionConfig.Webconfig.Rbmq_Host.Split(':');
3 years ago
options.HostName = _host[0];
if (_host.Length == 2)
{
options.Port = int.Parse(_host[1]);
}
3 years ago
options.UserName = sysOptionConfig.Webconfig.Rbmq_UserName;
options.Password = sysOptionConfig.Webconfig.Rbmq_Password;
3 years ago
});
x.UseDashboard();
});
}
3 years ago
3 years ago
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");
});
//<2F><>ʼ<EFBFBD><CABC>IOC
2 years ago
IOC.container = services;
3 years ago
}
// 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>();//<2F>ܵ<EFBFBD>
app.UseRouting();
3 years ago
3 years ago
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");//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
builder.AllowAnyOrigin();
});
app.UseAuthentication();//<2F><>֤
app.UseAuthorization();//<2F><>Ȩ
app.UseHangfireDashboard("/job", new DashboardOptions() { Authorization = new[] { new HangFireAuthorizeFilter() } });
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
2 years ago
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2 years ago
//_Job(env);
2 years ago
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
private void _Job(IWebHostEnvironment env)
{
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
var _itool = IOC.AddServer<IToolsService>();
// _itool.bindConfigDict(true, 5);
_itool.AutoJob();
3 years ago
}
}
}