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.

177 lines
6.3 KiB
C#

11 months ago
using Common;
11 months ago
using djy.Model;
11 months ago
using djy.Service.DjyService;
using djy_AfrApi.Filter;
using djy_AfrApi.Middlewares;
using FreeRedis;
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 Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
11 months ago
using System.IO;
using System;
11 months ago
using System.Linq;
using System.Reflection;
namespace djy_AfrApi
{
public class Startup
{
public Startup(IConfiguration configuration,IHostEnvironment hostEnvironment)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddScoped<IUser, AspNetUser>();
services.AddControllers(o =>
{
o.Filters.Add(typeof(GlobalExceptionsFilter));
})
.AddNewtonsoftJson(options =>
{
//<2F><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//<2F>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>շ<EFBFBD><D5B7><EFBFBD>ʽ<EFBFBD><CABD>key
//options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʽ
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//<2F><><EFBFBD><EFBFBD>Model<65><6C>Ϊnull<6C><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
//<2F><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UTCʱ<43><CAB1>
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
//<2F><><EFBFBD><EFBFBD>Enumתstring
options.SerializerSettings.Converters.Add(new StringEnumConverter());
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var result = new BadRequestObjectResult(context.HttpContext);
return result;
};
});
services.AddHttpClient();
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
Configuration.Bind("WebConfig", sysOptionConfig.Webconfig);
sysOptionConfig._Configuration = Configuration;
// ע<><D7A2>redis<69>ͻ<EFBFBD><CDBB><EFBFBD>
RedisClient cli = new RedisClient(sysOptionConfig.Webconfig.Redis + ",defaultDatabase=" + sysOptionConfig.Webconfig.RedisDb);
services.AddSingleton<IRedisClient>(cli);
services.AddAuthentication("Bearer").AddJwtBearer("Bearer", option =>
{
option.Authority = sysOptionConfig.Webconfig.IdentServerUrl;//<2F><>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>URl<52><6C>ַ
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);
}
}
services.AddAutoMapper(typeof(AutoMapperConfig));
services.AddSwaggerGen(c =>
{
11 months ago
c.SwaggerDoc("v1", new OpenApiInfo { Title = "AFR Api<70>ĵ<EFBFBD>", Version = "v1" });
var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "djy_AfrApi.xml");
c.IncludeXmlComments(xmlPath, true);
var xmlModelPath = Path.Combine(basePath, "djy.Model.xml");
c.IncludeXmlComments(xmlModelPath);
c.UseInlineDefinitionsForEnums();
11 months ago
});
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)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
11 months ago
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AFR Api<70>ĵ<EFBFBD> v1"));
11 months ago
}
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
app.UseRequRespLogMiddleware();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>Ӧ<EFBFBD><D3A6>ʽ<EFBFBD><CABD>401<30><31>403<30><33>
app.UseUnifyResultMiddleware();
// <20><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>
app.UseExceptionHandlerMiddle();
app.UseRouting();
//DefaultFilesOptions defaultFiles = new DefaultFilesOptions();
//defaultFiles.DefaultFileNames.Clear();
//defaultFiles.DefaultFileNames.Add("index.html");
//app.UseDefaultFiles(defaultFiles);
//app.UseStaticFiles();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
app.UseCors(builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
//builder.WithOrigins("http://www.baidu.com");//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
builder.AllowAnyOrigin();
});
//<2F><>֤
app.UseAuthentication();
//<2F><>Ȩ
app.UseAuthorization();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>浱ǰ<E6B5B1><C7B0>¼<EFBFBD><C2BC>User<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˺<EFBFBD>ISF<53><46>AMS<4D>߼<EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>£<EFBFBD>
app.UseNextAuthorizationMiddle();
11 months ago
// <20><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>
app.UseCommonCacheMiddleware();
11 months ago
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}