170 lines
5.9 KiB
C#
170 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Localization;
|
|
using Microsoft.AspNetCore.Mvc.Razor;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Sarand.Core.App;
|
|
using Sarand.Core.Base;
|
|
using Sarand.OperationsMgr.Core.App;
|
|
using Sarand.OperationsMgr.Web.System;
|
|
|
|
namespace Sarand.OperationsMgr.Web.App
|
|
{
|
|
//=======================================================================
|
|
public class Startup
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private SaOperationsMgrWebApp app = null;
|
|
// Localisation.
|
|
private static RequestCulture defaultRequestCulture = new RequestCulture("en-GB", "en-GB");
|
|
private static List<CultureInfo> supportedCultures = new List<CultureInfo>
|
|
{
|
|
new CultureInfo("en-GB"),
|
|
new CultureInfo("en-US"),
|
|
new CultureInfo("en"),
|
|
};
|
|
|
|
//-------------------------------------------------------------------
|
|
public Startup (IHostingEnvironment env)
|
|
{
|
|
String fn = "SaOperationsMgrWeb.Startup";
|
|
|
|
// Load server configuration.
|
|
String machineName = Environment.MachineName;
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(env.ContentRootPath)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.machine.{machineName}.json", optional: false, reloadOnChange: true)
|
|
.AddEnvironmentVariables();
|
|
Logger.Info(fn + " EnvironmentName: " + env.EnvironmentName + ". MachineName: " + machineName + ".");
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
|
|
builder.AddApplicationInsightsSettings(developerMode: true);
|
|
}
|
|
else
|
|
{
|
|
// Staging, Production.
|
|
}
|
|
Configuration = builder.Build();
|
|
|
|
// Avoid automatic binding of model properties.
|
|
// Ref. https://stackoverflow.com/questions/2211829/why-does-asp-net-mvc-care-about-my-read-only-properties-during-databinding
|
|
//DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
|
|
|
|
// Initialise application:
|
|
app = new SaOperationsMgrWebApp(env, Configuration.GetSection("SessionContexts"), Opn.ApplicationCaption + " Startup");
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
public IConfigurationRoot Configuration { get; }
|
|
|
|
//-------------------------------------------------------------------
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices (IServiceCollection services)
|
|
{
|
|
// Add framework services.
|
|
services.AddApplicationInsightsTelemetry(Configuration);
|
|
|
|
// Aug-2017 Added to support memory caching of sessions.
|
|
services.AddMemoryCache();
|
|
//services.AddDistributedMemoryCache(); - for multiple web servers.
|
|
services.AddSession
|
|
(options =>
|
|
{
|
|
/* Default cookie name: '.AspNetCore.Session' */
|
|
options.CookieName = "SaOperationsMgr.Session";
|
|
options.IdleTimeout = SacsSystem.DebugMode ? TimeSpan.FromMinutes(15) : TimeSpan.FromMinutes(30);
|
|
options.CookieHttpOnly = true;
|
|
}
|
|
);
|
|
|
|
// Application localisation.
|
|
services.AddSingleton<OpnLocaliser>();
|
|
// Lovalisation properties folder.
|
|
String resourcesPath = "Localisation";
|
|
services.AddLocalization(opts => { opts.ResourcesPath = resourcesPath; });
|
|
|
|
services.AddMvc
|
|
(options =>
|
|
{
|
|
// Model binding - intercepting form values being bound to models.
|
|
options.ModelBinderProviders.Insert(0, new OpnModelBinderProvider());
|
|
}
|
|
)
|
|
// View and data annotation localisations.
|
|
.AddViewLocalization(
|
|
LanguageViewLocationExpanderFormat.Suffix,
|
|
opts => { opts.ResourcesPath = resourcesPath; })
|
|
.AddDataAnnotationsLocalization();
|
|
|
|
// Configure localisation middleware.
|
|
services.Configure<RequestLocalizationOptions>
|
|
(opts =>
|
|
{
|
|
// Default culture to use for requests.
|
|
opts.DefaultRequestCulture = defaultRequestCulture;
|
|
// Cultures supported by the application. Formatting numbers, dates, etc.
|
|
opts.SupportedCultures = supportedCultures;
|
|
// UI cultures supported by the application. UI strings that have been localized.
|
|
opts.SupportedUICultures = supportedCultures;
|
|
}
|
|
);
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
loggerFactory.AddDebug();
|
|
|
|
// Application insights telemetry for requests.
|
|
//app.UseApplicationInsightsRequestTelemetry();
|
|
//app.UseApplicationInsightsExceptionTelemetry();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
//app.UseBrowserLink();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
}
|
|
|
|
// Enable static file serving.
|
|
app.UseStaticFiles();
|
|
|
|
// Localisation.
|
|
var localisationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
|
|
app.UseRequestLocalization(localisationOptions.Value);
|
|
|
|
// Initialise session handling.
|
|
app.UseSession();
|
|
|
|
app.UseMvc(routes =>
|
|
{
|
|
routes.MapRoute(
|
|
name: "default",
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
});
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|