You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.2 KiB

3 years ago
using Autofac;
using Autofac.Extensions.DependencyInjection;
using ce.autofac.extension;
using learun.application;
using learun.cache;
using learun.database;
using learun.iapplication;
using learun.operat;
using learun.util;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;
namespace learun.webapi
{
/// <summary>
///
/// </summary>
public class Startup
{
/// <summary>
///
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
///
/// </summary>
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
/// <summary>
///
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//����Swagger
//ע��Swagger������������һ��Swagger �ĵ�
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "�ӿ��ĵ�",
Description = "�������ݿ���Core��webapi"
});
//swagger�п���������ʱ�����Ƿ���Ҫ��url������accesstoken
c.OperationFilter<AddAuthTokenHeaderParameter>();
// Ϊ Swagger ����xml�ĵ�ע��·��
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//��ȡӦ�ó�������Ŀ¼
var xmlPath = Path.Combine(basePath, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
public void ConfigureContainer(ContainerBuilder builder)
{
// ���������ӷ���ע��
builder.RegisterIBLL();
DbRegister.Register(builder);
builder.RegisterType(typeof(CacheByRedis)).As(typeof(ICache)).SingleInstance();
builder.RegisterType(typeof(Operator)).As(typeof(IOperator)).SingleInstance();
builder.RegisterType(typeof(LogBLL)).As(typeof(LogIBLL)).SingleInstance();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// <summary>
///
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
IocManager.Instance.Container = app.ApplicationServices.GetAutofacRoot();
string baseDir = env.ContentRootPath;
ConfigHelper.SetValue("baseDir", baseDir);
if (env.IsDevelopment())
{
ConfigHelper.SetValue("env", "dev");
app.UseDeveloperExceptionPage();
}
else
{
ConfigHelper.SetValue("env", "pro");
app.UseExceptionHandler("/Home/Error");
}
//app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseMiddleware<AuthorizeMiddleware>();
//���������������Swagger
app.UseSwagger();
//�����м�����������SwaggerUI��ָ��Swagger JSON�ս���
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "learun.adms.webapi.v1");
c.RoutePrefix = string.Empty;//���ø��ڵ�����
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}