using Autofac; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; using System; using System.IO; using System.Reflection; using Wms.AutoMapperServer; using Wms.Filter; using Wms.Tools; using WMS.Entity.Context; using WMS.IBLL.IDataServer; using Serilog; using Autofac.Core; using Utility; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Http; using SqlSugar; using Utility.Job; namespace Wms { 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) { //Ï·¢WCS·¾¶Str var url = Configuration.GetSection("ApiUrlConfig:WcsHost").Value + Configuration.GetSection("ApiUrlConfig:IssueComApiUrl").Value; //Ï·¢AGV·¾¶Str var url2 = Configuration.GetSection("ApiUrlConfig:AgvHost").Value + Configuration.GetSection("ApiUrlConfig:IssueComApiUrl").Value; //µ÷Óö¨Ê±ÈÎÎñ services.AddHostedService(provider => new DailyTaskService(url, url2)); services.AddControllers(options => { options.Filters.Add(); options.Filters.Add(); }) .AddJsonOptions(options => { //²»Ê¹ÓÃÍÕ·åÑùʽµÄkey options.JsonSerializerOptions.PropertyNamingPolicy = null; } ); //È«¾ÖÅäÖÃJsonÐòÁл¯´¦Àí services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WMSºǫ́¹ÜÀí", Version = "v1" }); #region ¶ÁÈ¡xmlÐÅÏ¢ var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); #endregion var openApiSecurity = new OpenApiSecurityScheme { Description = "JWTÈÏÖ¤ÊÚȨ£¬Ê¹ÓÃÖ±½ÓÔÚÏ¿òÖÐÊäÈëBearer {token}£¨×¢ÒâÁ½ÕßÖ®¼äÊÇÒ»¸ö¿Õ¸ñ£©\"", Name = "token", //jwt ĬÈϲÎÊýÃû³Æ In = ParameterLocation.Header, //jwtĬÈÏ´æ·ÅAuthorizationÐÅÏ¢µÄλÖã¨ÇëÇóÍ·£© Type = SecuritySchemeType.ApiKey }; c.AddSecurityDefinition("oauth2", openApiSecurity); c.OperationFilter(); }); #region ¿çÓò´¦Àí services.AddCors(options => { options.AddPolicy(name: "MyCors", builder => { builder.AllowAnyMethod() .SetIsOriginAllowed(_ => true) .AllowAnyHeader() .AllowCredentials(); }); }); #endregion #region jwtÅäÖà services.AddTransient(); //¶ÁÈ¡ÅäÖÃÎļþÅäÖõÄjwtÏà¹ØÅäÖà services.Configure(Configuration.GetSection("JWTConfig")); //ÆôÓÃJWT services.AddAuthentication(Options => { Options.AddScheme(MyAuthHandler.SchemeName, "default scheme"); Options.DefaultAuthenticateScheme = MyAuthHandler.SchemeName; Options.DefaultChallengeScheme = MyAuthHandler.SchemeName; }).AddJwtBearer(); #endregion //¶ÁÈ¡ÅäÖÃÎļþÅäÖõĽӿڽ»»¥µÄÏà¹ØÅäÖà services.Configure(Configuration.GetSection("ApiUrlConfig")); //Êý¾Ý¿âÅäÖà BaseDbConfig.ConnectionString = Configuration.GetSection("AppSettings:ConnectionString").Value; //ÑéÇ©ÅäÖÃÎļþ SignConfig.ERPAppKey = Configuration.GetSection("SignConfig:ERPAppKey").Value; SignConfig.MESAppKey = Configuration.GetSection("SignConfig:MESAppKey").Value; SignConfig.LIMSAppKey = Configuration.GetSection("SignConfig:LIMSAppKey").Value; SignConfig.FuMaAppKey = Configuration.GetSection("SignConfig:FuMaAppKey").Value; SignConfig.Minutes = double.Parse(Configuration.GetSection("SignConfig:Minutes").Value); #region AutoMapper×¢Èë services.AddAutoMapper(typeof(AutoMapperProfile)); // automapperÒÀÀµ #endregion //×¢²áserilog services.AddConfigSerilog(); services.AddScoped(); services.AddScoped(); services.AddSingleton(DataContext.Db); // µ¥Àý×¢²á services.AddTransient(); // ÊÂÎñÓ빤×÷µ¥Ôª×¢²á services.AddHostedService(); services.AddSingleton(); services.AddTransient(); services.AddTransient(); } public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterAssemblyTypes(Assembly.Load("WMS.IDAL"), Assembly.Load("WMS.DAL"), Assembly.Load("WMS.IBLL"), Assembly.Load("WMS.BLL")) .InstancePerLifetimeScope() .AsImplementedInterfaces(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiDemo v1"); c.RoutePrefix = string.Empty; }); } //È«¾Ö·µ»Ø¹æ·¶ //app.UseApiResponse();//ÆúÓà ¸ÄÓÃFilter [ServiceFilter(typeof(ApiResponseActionFilter))] //ʹÓÃSerilog¼Ç¼ÇëÇóÈÕÖ¾ app.UseSerilogRequestLogging(); //app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseCors("MyCors");//¿çÓò app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }