Startup.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using Abp.AspNetCore;
  3. using Abp.AspNetCore.TestBase;
  4. using Abp.Dependency;
  5. using YGNT.Exam.Authentication.JwtBearer;
  6. using YGNT.Exam.Configuration;
  7. using YGNT.Exam.EntityFrameworkCore;
  8. using YGNT.Exam.Identity;
  9. using YGNT.Exam.Web.Resources;
  10. using YGNT.Exam.Web.Startup;
  11. using Castle.MicroKernel.Registration;
  12. using Microsoft.AspNetCore.Builder;
  13. using Microsoft.AspNetCore.Hosting;
  14. using Microsoft.EntityFrameworkCore;
  15. using Microsoft.Extensions.Configuration;
  16. using Microsoft.Extensions.DependencyInjection;
  17. using Microsoft.Extensions.Logging;
  18. namespace YGNT.Exam.Web.Tests
  19. {
  20. public class Startup
  21. {
  22. private readonly IConfigurationRoot _appConfiguration;
  23. public Startup(IWebHostEnvironment env)
  24. {
  25. _appConfiguration = env.GetAppConfiguration();
  26. }
  27. public IServiceProvider ConfigureServices(IServiceCollection services)
  28. {
  29. services.AddEntityFrameworkInMemoryDatabase();
  30. services.AddMvc();
  31. IdentityRegistrar.Register(services);
  32. AuthConfigurer.Configure(services, _appConfiguration);
  33. services.AddScoped<IWebResourceManager, WebResourceManager>();
  34. //Configure Abp and Dependency Injection
  35. return services.AddAbp<ExamWebTestModule>(options =>
  36. {
  37. options.SetupTest();
  38. });
  39. }
  40. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
  41. {
  42. UseInMemoryDb(app.ApplicationServices);
  43. app.UseAbp(); //Initializes ABP framework.
  44. app.UseExceptionHandler("/Error");
  45. app.UseStaticFiles();
  46. app.UseRouting();
  47. app.UseAuthentication();
  48. app.UseJwtTokenMiddleware();
  49. app.UseAuthorization();
  50. app.UseEndpoints(endpoints =>
  51. {
  52. endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
  53. });
  54. }
  55. private void UseInMemoryDb(IServiceProvider serviceProvider)
  56. {
  57. var builder = new DbContextOptionsBuilder<ExamDbContext>();
  58. builder.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseInternalServiceProvider(serviceProvider);
  59. var options = builder.Options;
  60. var iocManager = serviceProvider.GetRequiredService<IIocManager>();
  61. iocManager.IocContainer
  62. .Register(
  63. Component.For<DbContextOptions<ExamDbContext>>()
  64. .Instance(options)
  65. .LifestyleSingleton()
  66. );
  67. }
  68. }
  69. }