jy-plc/Startup.cs

164 lines
5.6 KiB
C#
Raw Permalink Normal View History

2024-07-24 13:30:21 +00:00
using AutoMapper;
using LangGuan.Command.AutoMapper;
using LangGuan.Command.Extension;
using LangGuan.Configuration;
using LangGuan.Middleware;
using LangGuan.Services;
using LangGuan.Services.Job;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace LangGuan
{
public class Startup
{
public Startup(IConfiguration configuration)
{
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.AddControllers(options =>
{
options.Filters.Add<CustomerExceptionFilter>();
}).AddNewtonsoftJson(option =>
{
//option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
//option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
}).AddJsonOptions(option =>
{
//userName
option.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
services.AddMemoryCache();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַת<D6B7><D7AA>Сд
services.AddRouting(options => options.LowercaseUrls = true);
#region ʹ<EFBFBD><EFBFBD>AutoMapper
var mapperConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new CustomProfile());
});
IMapper mapper = mapperConfig.CreateMapper();
services.AddSingleton(mapper);
#endregion
//cors
services.AddCors(options =>
{
options.AddPolicy("cors", builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.AllowAnyHeader();
});
});
//<2F><EFBFBD><E6B1BE>
#region API versioning
services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
#endregion
//Swagger <20><><EFBFBD><EFBFBD>
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, SwaggerConfigureOptions>();
services.AddSwaggerGen(options =>
{
options.IncludeXmlComments(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LangGuan.xml"));
});
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
services.AddTransient<Hj212Service>();
services.AddTransient<EmployeeService>();
services.AddTransient<AlertService>();
services.AddTransient<DeviceSerive>();
services.AddTransient<VideoService>();
services.AddTransient<RadarService>();
services.AddTransient<GroupService>();
services.AddTransient<RadarItemService>(); //<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
services.AddTransient<GroupService>();
services.AddTransient<PlcHelper2>();
services.AddTransient<PlcHelper>();
services.AddTransient<DeviceUsedService>();
services.AddSingleton<MyJob>();
services.AddJobService();
#endregion
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
IApiVersionDescriptionProvider apiVersionDescriptionProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseCors("cors");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseSwagger(options =>
{
options.PreSerializeFilters.Add((swagger, req) =>
{
swagger.Servers = new List<OpenApiServer>() { new OpenApiServer() { Url = $"http://{req.Host}" } };
});
});
app.UseSwaggerUI(options =>
{
foreach (var desc in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
//<2F><><EFBFBD><EFBFBD>swagger.json
options.SwaggerEndpoint($"/swagger/{desc.GroupName}/swagger.json", desc.ApiVersion.ToString());
options.DefaultModelsExpandDepth(-1);
options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
//<2F><><EFBFBD><EFBFBD>swagger<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ
options.RoutePrefix = "api/LangGuan";
}
});
}
}
}