using Domain.AggregateModel.AppAggregate; using Domain.AggregateModel.LinkAggregate; using FluentValidation; using FluentValidation.AspNetCore; using Infrastructure; using Infrastructure.Repositories; using MediatR; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Pomelo.EntityFrameworkCore.MySql; using Pomelo.EntityFrameworkCore.MySql.Infrastructure; using QRCodeService.Application.Behaviors; using QRCodeService.Application.Commands; using QRCodeService.Application.Queries; using QRCodeService.Application.Validations; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace QRCodeService { 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.AddControllersWithViews(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" }); }); //validator services.AddTransient, CreateLinkCommandValidator>(); //MediatR+ services.AddMediatR(Assembly.GetExecutingAssembly()) .AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)) .AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>)) .AddTransient(typeof(IPipelineBehavior<,>), typeof(TransactionBehaviour<,>)); //EFCore services.AddDbContext( dbContextOptions => dbContextOptions .UseMySql( "server=localhost;user=root;password=root;database=qrcode", // For common usages, see pull request #1233. new MariaDbServerVersion(new Version(10, 5, 9)), // use MariaDbServerVersion for MariaDB mySqlOptions => mySqlOptions .CharSetBehavior(CharSetBehavior.NeverAppend)) // Everything from this point on is optional but helps with debugging. .EnableSensitiveDataLogging() .EnableDetailedErrors() ); //Repository services.AddScoped(); services.AddScoped(); services.AddScoped(); } // 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", "QRCodeService v1")); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }