Waste/Waste.Web.Core/Startup.cs

140 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Nirvana.Common;
using Serilog;
using System;
using System.IO;
using System.Text;
using Waste.Core;
namespace Waste.Web.Core
{
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
var configuration = App.Configuration;
services.AddSession();//使用Session
//services.AddJwt<JwtHandler>(options =>
//{
// options.DefaultScheme = null;
//});
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
services.AddCorsAccessor();
services.AddRemoteRequest();
services.AddHttpClient();
//添加CAP
string port = configuration["RabbitmqSetting:Port"];
int p = Convert.ToInt32(port);
if (p > 0)
{
services.AddCap(x =>
{
x.DefaultGroupName = "jtsky.queue.waste"; //rabbitmq的队列名称
//配置rabbitmq支持
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"]);
x.SucceedMessageExpiredAfter = 3600;
x.UseDashboard();
});
}
services.AddControllers(options =>
{
options.EnableEndpointRouting = true;
})
.AddInjectWithUnifyResult<RESTfulResultProvider>();
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();
#region IP
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
MyHttpContext.serviceCollection = services;
#endregion
//添加redis缓存
services.AddStackExchangeRedisCache(options =>
{
// 连接字符串,这里也可以读取配置文件
options.Configuration = "localhost";
// 键名前缀
options.InstanceName = "jt_";
});
//添加即时通讯
// services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//启用 GB2312按需
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
app.UseSession();
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
// 添加规范化结果状态码,需要在这里注册
app.UseUnifyResultStatusCodes();
// 或者更多配置
// app.UseUnifyResultStatusCodes(options => { options.Return200StatusCodes = new [] { 401, 403 }; });
//允许body重用
app.Use(next => context =>
{
context.Request.EnableBuffering();
return next(context);
});
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSerilogRequestLogging(opts =>
{
opts.EnrichDiagnosticContext = LoggerHelper.EnrichFromRequest;
}); // 必须在 UseStaticFiles 和 UseRouting 之间,记录请求日志
app.UseRouting();
app.UseCorsAccessor();
// app.UseAuthentication();
app.UseAuthorization();
app.UseInject();
app.UseEndpoints(endpoints =>
{
//注册集线器
// endpoints.MapHubs();
//endpoints.MapGet("/index.html", async context => {
// await context.Response.WriteAsync("Hello");
//});
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}