jy-plc/Startup.cs

164 lines
5.6 KiB
C#
Raw Permalink 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 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();
//ÇëÇóµØÖ·×ª³ÉСд
services.AddRouting(options => options.LowercaseUrls = true);
#region ʹÓÃ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();
});
});
//°æ±¾ºÅ
#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 ÅäÖÃ
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, SwaggerConfigureOptions>();
services.AddSwaggerGen(options =>
{
options.IncludeXmlComments(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LangGuan.xml"));
});
#region ·þÎñ×¢Èë
services.AddTransient<Hj212Service>();
services.AddTransient<EmployeeService>();
services.AddTransient<AlertService>();
services.AddTransient<DeviceSerive>();
services.AddTransient<VideoService>();
services.AddTransient<RadarService>();
services.AddTransient<GroupService>();
services.AddTransient<RadarItemService>(); //¶¨Ê±ÈÎÎñ
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)
{
//ÅäÖÃswagger.json
options.SwaggerEndpoint($"/swagger/{desc.GroupName}/swagger.json", desc.ApiVersion.ToString());
options.DefaultModelsExpandDepth(-1);
options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
//ÅäÖÃswagger·ÃÎÊÊ×Ò³
options.RoutePrefix = "api/LangGuan";
}
});
}
}
}