Waste/Waste.Web.Core/Startup.cs

138 lines
5.1 KiB
C#
Raw Normal View History

2021-04-30 14:52:42 +08:00
using Furion;
using Microsoft.AspNetCore.Builder;
2021-05-27 16:58:40 +08:00
using Microsoft.AspNetCore.DataProtection;
2021-04-30 14:52:42 +08:00
using Microsoft.AspNetCore.Hosting;
2021-05-27 16:58:40 +08:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
2021-08-02 17:48:22 +08:00
using Microsoft.AspNetCore.StaticFiles;
2021-04-30 14:52:42 +08:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2021-05-27 16:58:40 +08:00
using Nirvana.Common;
2021-04-30 14:52:42 +08:00
using Serilog;
using System;
2021-08-02 17:48:22 +08:00
using System.Collections.Generic;
2021-05-27 16:58:40 +08:00
using System.IO;
2022-03-28 17:19:48 +08:00
using System.Text;
2021-05-27 16:58:40 +08:00
using System.Threading.Tasks;
using Waste.Core;
2021-04-30 14:52:42 +08:00
namespace Waste.Web.Core
{
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
var configuration = App.Configuration;
2021-05-27 16:58:40 +08:00
services.AddSession();//使用Session
//services.AddJwt<JwtHandler>(options =>
//{
// options.DefaultScheme = null;
//});
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
2021-04-30 14:52:42 +08:00
services.AddCorsAccessor();
// services.AddRemoteRequest();
2021-05-27 16:58:40 +08:00
services.AddHttpClient();
//添加CAP
2021-11-23 17:49:40 +08:00
services.AddCap(x =>
{
x.DefaultGroupName = "jtsky.queue.waste"; //rabbitmq的队列名称
//配置rabbitmq支持
string port = configuration["RabbitmqSetting:Port"];
int p = Convert.ToInt32(port);
2021-11-23 17:49:40 +08:00
x.UseRabbitMQ(opt =>
{
opt.HostName = configuration["RabbitmqSetting:HostName"]; //配置ip地址
opt.Port = p;//配置端口
opt.UserName = configuration["RabbitmqSetting:UserName"];//配置用户名
opt.Password = configuration["RabbitmqSetting:Password"];//配置Miami
});
//配置sqlserver支持
x.UseSqlServer(configuration["RabbitmqSetting:DBConnection"]);
});
2021-11-23 17:49:40 +08:00
services.AddControllers(options =>
{
2021-05-27 16:58:40 +08:00
options.EnableEndpointRouting = true;
})
2021-04-30 14:52:42 +08:00
.AddInjectWithUnifyResult<RESTfulResultProvider>();
2021-05-27 16:58:40 +08:00
services.AddRazorPages(options =>
{
options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //日期格式化
options.SerializerSettings.ContractResolver = new CustomContractResolver();
});
services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
services.AddTaskScheduler();
2021-05-27 16:58:40 +08:00
#region IP
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
MyHttpContext.serviceCollection = services;
#endregion
2022-03-28 17:19:48 +08:00
//添加redis缓存
services.AddStackExchangeRedisCache(options =>
{
// 连接字符串,这里也可以读取配置文件
options.Configuration = "localhost";
// 键名前缀
options.InstanceName = "jt_";
});
2021-08-11 08:16:16 +08:00
//添加即时通讯
2021-11-23 17:49:40 +08:00
// services.AddSignalR();
2021-04-30 14:52:42 +08:00
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2022-03-28 17:19:48 +08:00
//启用 GB2312按需
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
2021-05-27 16:58:40 +08:00
app.UseSession();
2021-04-30 14:52:42 +08:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2021-05-27 16:58:40 +08:00
// 添加规范化结果状态码,需要在这里注册
app.UseUnifyResultStatusCodes();
// 或者更多配置
// app.UseUnifyResultStatusCodes(options => { options.Return200StatusCodes = new [] { 401, 403 }; });
//允许body重用
app.Use(next => context =>
{
context.Request.EnableBuffering();
return next(context);
});
2021-04-30 14:52:42 +08:00
app.UseHttpsRedirection();
2021-05-27 16:58:40 +08:00
app.UseStaticFiles();
2021-11-23 17:49:40 +08:00
app.UseSerilogRequestLogging(opts =>
{
opts.EnrichDiagnosticContext = LoggerHelper.EnrichFromRequest;
}); // 必须在 UseStaticFiles 和 UseRouting 之间,记录请求日志
2021-04-30 14:52:42 +08:00
app.UseRouting();
app.UseCorsAccessor();
2021-11-23 17:49:40 +08:00
// app.UseAuthentication();
2021-04-30 14:52:42 +08:00
app.UseAuthorization();
2021-05-27 16:58:40 +08:00
app.UseInject();
2021-04-30 14:52:42 +08:00
2021-08-11 08:16:16 +08:00
2021-04-30 14:52:42 +08:00
app.UseEndpoints(endpoints =>
{
2021-08-11 08:16:16 +08:00
//注册集线器
2021-11-23 17:49:40 +08:00
// endpoints.MapHubs();
2021-05-27 16:58:40 +08:00
//endpoints.MapGet("/index.html", async context => {
// await context.Response.WriteAsync("Hello");
//});
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
2021-04-30 14:52:42 +08:00
});
}
}
}