Last active
October 13, 2015 05:38
-
-
Save DamianEdwards/488cbfa65ec2647b2812 to your computer and use it in GitHub Desktop.
ASP.NET 5 ApplicationStartup base class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.AspNet.Builder; | |
| using Microsoft.Dnx.Runtime; | |
| using Microsoft.Extensions.Configuration; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Logging; | |
| namespace Microsoft.AspNet.Hosting | |
| { | |
| /// <summary> | |
| /// Defines the methods and properties used to configure and start an ASP.NET 5 application. | |
| /// </summary> | |
| public abstract class ApplicationStartup | |
| { | |
| public ApplicationStartup() | |
| { | |
| ApplicationEnvironment = RuntimeServices.ApplicationEnvironment; | |
| RuntimeEnvironment = RuntimeServices.RuntimeEnvironment; | |
| var configBuilder = new ConfigurationBuilder(); | |
| configBuilder.AddEnvironmentVariables(); | |
| var config = configBuilder.Build(); | |
| HostingEnvironment = new HostingEnvironment(); | |
| HostingEnvironment.Initialize(ApplicationEnvironment.ApplicationBasePath, config[WebHostBuilder.EnvironmentKey]); | |
| } | |
| /// <summary> | |
| /// Provides information about the current application. | |
| /// </summary> | |
| public IApplicationEnvironment ApplicationEnvironment { get; private set; } | |
| /// <summary> | |
| /// Provides information about the current application runtime, including the operating system and architecture. | |
| /// </summary> | |
| public IRuntimeEnvironment RuntimeEnvironment { get; private set; } | |
| /// <summary> | |
| /// Provides information about the application's hosting environment, including the configured environment name, | |
| /// e.g. "Development" or "Production". | |
| /// </summary> | |
| public IHostingEnvironment HostingEnvironment { get; private set; } | |
| /// <summary> | |
| /// Configures the application's service container. | |
| /// </summary> | |
| /// <param name="services"> | |
| /// The collection of services the application uses to build the service container. | |
| /// </param> | |
| /// <returns> | |
| /// The application's service container. | |
| /// This can be built by calling the <c>BuildServiceProvider</c> extension method on the passed <see cref="IServiceCollection"/>. | |
| /// </returns> | |
| public virtual IServiceProvider ConfigureServices(IServiceCollection services) | |
| { | |
| return services.BuildServiceProvider(); | |
| } | |
| /// <summary> | |
| /// Configures the application's HTTP request pipeline and logging. | |
| /// </summary> | |
| /// <param name="app">Used to configure the HTTP request pipeline and access application services from the service container.</param> | |
| /// <param name="loggerFactory">Used to configure logging providers.</param> | |
| public abstract void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNet.Builder; | |
| using Microsoft.AspNet.Diagnostics.Entity; | |
| using Microsoft.AspNet.Hosting; | |
| using Microsoft.AspNet.Identity.EntityFramework; | |
| using Microsoft.Data.Entity; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Configuration; | |
| using Microsoft.Extensions.Logging; | |
| using WebApplication4.Models; | |
| using WebApplication4.Services; | |
| namespace WebApplication4 | |
| { | |
| public class Startup : ApplicationStartup | |
| { | |
| public Startup() | |
| { | |
| // Setup configuration sources. | |
| var builder = new ConfigurationBuilder() | |
| .SetBasePath(ApplicationEnvironment.ApplicationBasePath) | |
| .AddJsonFile("config.json") | |
| .AddJsonFile($"config.{HostingEnvironment.EnvironmentName}.json", optional: true); | |
| if (HostingEnvironment.IsDevelopment()) | |
| { | |
| // This reads the configuration keys from the secret store. | |
| // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 | |
| builder.AddUserSecrets(); | |
| } | |
| builder.AddEnvironmentVariables(); | |
| Configuration = builder.Build(); | |
| } | |
| public IConfigurationRoot Configuration { get; set; } | |
| // This method gets called by the runtime. Use this method to add services to the container. | |
| public override IServiceProvider ConfigureServices(IServiceCollection services) | |
| { | |
| // Add Entity Framework services to the services container. | |
| services.AddEntityFramework() | |
| .AddSqlServer() | |
| .AddDbContext<ApplicationDbContext>(options => | |
| options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); | |
| // Add Identity services to the services container. | |
| services.AddIdentity<ApplicationUser, IdentityRole>() | |
| .AddEntityFrameworkStores<ApplicationDbContext>() | |
| .AddDefaultTokenProviders(); | |
| // Add MVC services to the services container. | |
| services.AddMvc(); | |
| // Register application services. | |
| services.AddTransient<IEmailSender, AuthMessageSender>(); | |
| services.AddTransient<ISmsSender, AuthMessageSender>(); | |
| return services.BuildServiceProvider(); | |
| } | |
| // Configure is called after ConfigureServices is called. | |
| public override void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) | |
| { | |
| loggerFactory.MinimumLevel = LogLevel.Information; | |
| loggerFactory.AddConsole(); | |
| loggerFactory.AddDebug(); | |
| // Configure the HTTP request pipeline. | |
| // Add the following to the request pipeline only in development environment. | |
| if (HostingEnvironment.IsDevelopment()) | |
| { | |
| //app.UseBrowserLink(); | |
| app.UseDeveloperExceptionPage(); | |
| app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll); | |
| } | |
| else | |
| { | |
| // Add Error handling middleware which catches all application specific errors and | |
| // sends the request to the following path or controller action. | |
| app.UseExceptionHandler("/Home/Error"); | |
| } | |
| // Add static files to the request pipeline. | |
| app.UseStaticFiles(); | |
| // Add cookie-based authentication to the request pipeline. | |
| app.UseIdentity(); | |
| // Add MVC to the request pipeline. | |
| app.UseMvc(routes => | |
| { | |
| routes.MapRoute( | |
| name: "default", | |
| template: "{controller=Home}/{action=Index}/{id?}"); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment