From b43e22588f86f4d4c6de428f0955f5f27775bca1 Mon Sep 17 00:00:00 2001 From: phillipfisher Date: Fri, 24 Jun 2016 14:07:20 -0500 Subject: [PATCH] Added support to WebApp to be ran directly without having to deploy to Service Fabric. Set WebApp as StartUp Project and NoFabric as Launch Profile and Run. --- WebApp/Program.cs | 37 ++++++++++---- WebApp/Properties/launchSettings.json | 6 +-- WebApp/Startup.cs | 17 +------ WebApp/StartupDevelopment.cs | 73 +++++++++++++++++++++++++++ WebApp/project.json | 3 +- WebApp/web.config | 9 ++++ 6 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 WebApp/StartupDevelopment.cs create mode 100644 WebApp/web.config diff --git a/WebApp/Program.cs b/WebApp/Program.cs index 07200f8..74f065f 100644 --- a/WebApp/Program.cs +++ b/WebApp/Program.cs @@ -9,22 +9,39 @@ public class Program { public static void Main(string[] args) { - var communicationContext = CreateAspNetCoreCommunicationContext(); + var localWebHost = CreateLocalWebHost(); + IHostingEnvironment env = localWebHost.Services.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment; - ServiceRuntime.RegisterServiceAsync("WebAppType", serviceContext => new WebAppService(serviceContext, communicationContext)).GetAwaiter().GetResult(); + if (env.IsDevelopment()) + { + localWebHost.Run(); + } + else + { + var communicationContext = new AspNetCoreCommunicationContext(CreateServiceFabricWebHost()); - communicationContext.WebHost.Run(); + ServiceRuntime.RegisterServiceAsync("WebAppType", serviceContext => new WebAppService(serviceContext, communicationContext)).GetAwaiter().GetResult(); + + communicationContext.WebHost.Run(); + } } - private static AspNetCoreCommunicationContext CreateAspNetCoreCommunicationContext() + private static IWebHost CreateLocalWebHost() { - var webHost = new WebHostBuilder().UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseServiceFabricEndpoint("WebAppTypeEndpoint") - .Build(); + return new WebHostBuilder().UseKestrel() + .UseUrls("http://localhost:8001") + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup("WebApp") + .Build(); + } - return new AspNetCoreCommunicationContext(webHost); + private static IWebHost CreateServiceFabricWebHost() + { + return new WebHostBuilder().UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseServiceFabricEndpoint("WebAppTypeEndpoint") + .Build(); } } } diff --git a/WebApp/Properties/launchSettings.json b/WebApp/Properties/launchSettings.json index 3fa38e9..9fe07cb 100644 --- a/WebApp/Properties/launchSettings.json +++ b/WebApp/Properties/launchSettings.json @@ -3,7 +3,7 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:27904/", + "applicationUrl": "http://localhost:8001/", "sslPort": 0 } }, @@ -15,10 +15,10 @@ "ASPNETCORE_ENVIRONMENT": "Development" } }, - "WebApp": { + "NoFabric": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:5000", + "launchUrl": "http://localhost:8001/", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/WebApp/Startup.cs b/WebApp/Startup.cs index 7e13a77..4e4b438 100644 --- a/WebApp/Startup.cs +++ b/WebApp/Startup.cs @@ -20,12 +20,6 @@ public Startup(IHostingEnvironment env) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); - if (env.IsDevelopment()) - { - // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 - builder.AddUserSecrets(); - } - builder.AddEnvironmentVariables(); Configuration = builder.Build(); } @@ -58,16 +52,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseDatabaseErrorPage(); - app.UseBrowserLink(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } + app.UseExceptionHandler("/Home/Error"); app.UseStaticFiles(); diff --git a/WebApp/StartupDevelopment.cs b/WebApp/StartupDevelopment.cs new file mode 100644 index 0000000..823bd5c --- /dev/null +++ b/WebApp/StartupDevelopment.cs @@ -0,0 +1,73 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using WebApp.Data; +using WebApp.Models; +using WebApp.Services; + +namespace WebApp +{ + public class StartupDevelopment + { + public StartupDevelopment(IHostingEnvironment env) + { + var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); + + // 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; } + + // 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.AddDbContext(options => + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + + services.AddIdentity() + .AddEntityFrameworkStores() + .AddDefaultTokenProviders(); + + services.AddMvc(); + + // Add application services. + services.AddTransient(); + services.AddTransient(); + } + + // 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(); + + app.UseDeveloperExceptionPage(); + app.UseDatabaseErrorPage(); + app.UseBrowserLink(); + + app.UseStaticFiles(); + + app.UseIdentity(); + + // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 + + app.UseMvc(routes => + { + routes.MapRoute( + name: "default", + template: "{controller=Home}/{action=Index}/{id?}"); + }); + } + } +} diff --git a/WebApp/project.json b/WebApp/project.json index cd8337d..1a14d29 100644 --- a/WebApp/project.json +++ b/WebApp/project.json @@ -1,4 +1,4 @@ -{ +{ "userSecretsId": "aspnet-WebApp-4b332dff-6daa-4b2c-a482-ddbf5b11741d", "dependencies": { @@ -11,6 +11,7 @@ "version": "1.0.0-preview1-final", "type": "build" }, + "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final", "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final", diff --git a/WebApp/web.config b/WebApp/web.config new file mode 100644 index 0000000..e04a039 --- /dev/null +++ b/WebApp/web.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file