代码拉取完成,页面将自动刷新
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SocketService.Hub;
using StackExchange.Redis;
using Swashbuckle.AspNetCore.Swagger;
namespace SocketService
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", true, true) // 启用 不同环境的配置文件
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true);
builder.AddEnvironmentVariables();
Configuration = builder.Build(); // 构建自定义的配置
Log.Logger = new LoggerConfiguration() // 注册日志组件
.WriteTo
.File("logs/.log", rollingInterval: RollingInterval.Day)
.CreateLogger();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });
});
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>(); // 注入http请求的上下文
services.AddSignalR(options =>
{
options.HandshakeTimeout = TimeSpan.FromSeconds(30); // 握手30s超时
options.EnableDetailedErrors = true;
})
.AddRedis(options =>
{
options.ConnectionFactory = async factory =>
{
var connection = await ConnectionMultiplexer.ConnectAsync(Configuration.GetConnectionString("Redis"));
connection.InternalError += (sender, e) =>
{
Log.Error("RedisInternalError({sender},{e})", sender, e);
};
connection.ErrorMessage += (sender, e) =>
{
Log.Error("RedisErrorMessage({sender},{e})", sender, e);
};
return connection;
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UsePathBase("/api"); // api使用后缀
if (env.IsDevelopment()) // 开发环境
{
app.UseDeveloperExceptionPage(); // 启用错误详情页
app.UseSwagger(); // 启用swagger
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/api/swagger/v1/swagger.json", "API_V1");
});
}
app.UseCors(x => // 跨域配置
x.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins(Configuration.GetConnectionString("AllowOrigins"))
.AllowCredentials());
// app.UseHttpsRedirection(); 需要https时
app.UseMvc();
app.UseDefaultFiles(); // 测试文件
app.UseStaticFiles();
app.UseSignalR(builder =>
{
builder.MapHub<MessageHub>("/message"); // 绑定socket 二级message
});
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。