Advertisement
Guest User

ASP.NET Core 2.2 DI - Troubles

a guest
Mar 28th, 2019
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. ****************************** Program.cs
  2.  
  3. using Microsoft.AspNetCore;
  4. using Microsoft.AspNetCore.Hosting;
  5.  
  6. namespace NCHInternalWebSite
  7. {
  8. public class Program
  9. {
  10. public static void Main(string[] args)
  11. {
  12. //var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
  13. //XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
  14.  
  15. CreateWebHostBuilder(args).Build().Run();
  16. }
  17.  
  18. // -------------- Exception thrown here
  19. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  20. WebHost.CreateDefaultBuilder(args)
  21. .UseStartup<Startup>();
  22. }
  23. }
  24.  
  25. ****************************** Startup.cs
  26.  
  27. using Microsoft.AspNetCore.Builder;
  28. using Microsoft.AspNetCore.Hosting;
  29. using Microsoft.AspNetCore.Http;
  30. using Microsoft.AspNetCore.Mvc;
  31. using Microsoft.Extensions.Configuration;
  32. using Microsoft.Extensions.DependencyInjection;
  33. using Microsoft.Extensions.Logging;
  34.  
  35. namespace NCHInternalWebSite
  36. {
  37. public class Startup
  38. {
  39. private readonly ILogger _logger;
  40.  
  41. public Startup(IConfiguration configuration, ILogger<Startup> logger)
  42. {
  43. Configuration = configuration;
  44. _logger = logger;
  45.  
  46. _logger.LogInformation("Starting web application . . .");
  47. }
  48.  
  49. public IConfiguration Configuration { get; }
  50.  
  51. // This method gets called by the runtime. Use this method to add services to the container.
  52. public void ConfigureServices(IServiceCollection services)
  53. {
  54. services.Configure<CookiePolicyOptions>(options =>
  55. {
  56. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  57. options.CheckConsentNeeded = context => true;
  58. options.MinimumSameSitePolicy = SameSiteMode.None;
  59. });
  60.  
  61. services.AddLogging(logging =>
  62. {
  63. logging.AddConsole();
  64. logging.AddDebug();
  65. });
  66.  
  67. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  68.  
  69. _logger.LogInformation("Configured Services . . .");
  70. }
  71.  
  72. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  73. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggingBuilder loggingBuilder)
  74. {
  75. if (env.IsDevelopment())
  76. {
  77. app.UseDeveloperExceptionPage();
  78. }
  79. else
  80. {
  81. app.UseExceptionHandler("/Home/Error");
  82. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  83. app.UseHsts();
  84. }
  85.  
  86. app.UseHttpsRedirection();
  87. app.UseStaticFiles();
  88. app.UseCookiePolicy();
  89.  
  90. // TODO: Configure logging via dependency injection?
  91. //loggingBuilder.AddLog4Net();
  92. loggingBuilder.AddDebug();
  93. loggingBuilder.AddConsole();
  94.  
  95.  
  96. // TODO: Is CORS worth it? Cross site forgery anti token will tank .net core applications without a policy in place first
  97. // https://weblog.west-wind.com/posts/2016/sep/26/aspnet-core-and-cors-gotchas
  98.  
  99. app.UseMvc(routes =>
  100. {
  101. routes.MapRoute(
  102. name: "default",
  103. template: "{controller=Login}/{action=Index}/");
  104. });
  105.  
  106. _logger.LogInformation("Configuring . . .");
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement