commodore73

Sample Startup Class for Web Projects

Jul 24th, 2020
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Deliverystack.Core.Stackcontent.Models.Repositories;
  2.  
  3. namespace Csweb
  4. {
  5.     using System;
  6.  
  7.     using Microsoft.AspNetCore.Builder;
  8.     using Microsoft.AspNetCore.Hosting;
  9.     using Microsoft.Extensions.Configuration;
  10.     using Microsoft.Extensions.DependencyInjection;
  11.     using Microsoft.Extensions.Hosting;
  12.    
  13.     using Contentstack.AspNetCore;
  14.  
  15.     using Deliverystack.Core;
  16.     using Deliverystack.Core.Stackcontent.Configuration;
  17.  
  18.     using Root.Core.Models.Configuration;
  19.     using Microsoft.AspNetCore.Hosting.Server.Features;
  20.     using System.Linq;
  21.     using Deliverystack.Core.Models.Repositories;
  22.     using System.Threading;
  23.  
  24.     public class Startup
  25.     {
  26.         private string _baseUrl = null;
  27.  
  28.         public Startup(IConfiguration configuration)
  29.         {
  30.             Configuration = configuration;
  31.         }
  32.  
  33.         private ContentstackRepository Repository { get; set; }
  34.  
  35.         public IConfiguration Configuration { get; }
  36.  
  37.         // This method gets called by the runtime. Use this method to add services to the container.
  38.         public void ConfigureServices(IServiceCollection services)
  39.         {
  40.             services.AddControllersWithViews();
  41.             services.AddSerializationConfigurator(Configuration);
  42.             services.AddContentstack(Configuration);
  43.  
  44.             try
  45.             {
  46.                 services.AddRepository(Configuration);
  47.                 ServiceProvider serviceProvider = services.BuildServiceProvider();
  48.                 Repository = (ContentstackRepository) serviceProvider.GetService<IRepository>();
  49.                 Repository.Configuration.BaseUrl = _baseUrl;
  50.  
  51.                 //TODO: automatic?                serviceProvider.Dispose();
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 new Instrument().Exception(this, ex, "exception adding repository");
  56.             }
  57.         }
  58.  
  59. //                private string _listening = null;
  60.  
  61.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  62.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
  63.         {
  64.             //TODO: fails on Azure
  65.             if (app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.Any())
  66.             {
  67.                 _baseUrl = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First();
  68.             }
  69.  
  70.             if (Repository != null)
  71.             {
  72.                 Repository.Configuration.BaseUrl = _baseUrl;
  73.             }
  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.UseRouting();
  89.             app.UseAuthorization();
  90.  
  91.             //            app.UseStatusCodePages();
  92.             app.UseEndpoints(endpoints =>
  93.             {
  94.                 endpoints.MapControllerRoute(
  95.                     name: "sitemap",
  96.                     "sitemap.xml",
  97.                     new { controller = "SitemapXml", action = "SiteMap" }
  98.                 );
  99.  
  100.                 if (env.IsDevelopment())
  101.                 {
  102.                     endpoints.MapControllerRoute(
  103.                         name: "default",
  104.                         pattern: "Contentstack/{action}",
  105.                         defaults: new {controller = "Contentstack", action = "ContentTypes"});
  106.                 }
  107.  
  108.                 endpoints.MapControllerRoute(
  109.                     name: "entry",
  110.                     pattern: "{*url}",
  111.                     new {controller = "Entry", action = "Index"});
  112.             });
  113.  
  114.             appLifetime.ApplicationStarted.Register(() =>
  115.             {
  116.                 new Instrument().Track(this, "ApplicationStarted : " + _baseUrl);
  117.             });
  118.  
  119.             appLifetime.ApplicationStopping.Register(() =>
  120.             {
  121.                 new Instrument().Track(this, "ApplicationStopping");
  122.             });
  123.  
  124.             appLifetime.ApplicationStopped.Register(() =>
  125.             {
  126.                 new Instrument().Track(this, "ApplicationStopped");
  127.             });
  128.  
  129.             if (Repository.Configuration.PreloadOutputCaches)
  130.             {
  131.                 Thread thread = new Thread(() => { Repository.PreloadOutputCaches(); });
  132.                 thread.IsBackground = true;
  133.                 thread.Start();
  134.             }
  135.         }
  136.     }
  137. }
RAW Paste Data