Deploying and hosting ASP.NET Core 2.0 web applications

Enviroment

My local development environment is a Windows 10 PC using Visual Studio 2017.

NOTE. Currently, I can only access my web server through cpanel. However, if the hosting provider cannot provide adequate support for the ASP.NET Core 2.0 application, I will look at the change to a dedicated server, where I have full access to the server and IIS.

My requirements / project

I am developing an ASP.NET web application targeting the .NET Core 2.0 platform and the application works fine with localhost and IIS express.

Now that the application is complete, I need to deploy and host it, which seems like a daunting task that will add years to my age over several days and weeks.

I read about users who went through a lot of articles, blogs, MS documents, stackoverflow, etc. (often unsuccessfully) for deployment on both Intranet and the Internet. Now I feel that publishing and deploying a .net kernel seems like a complete mass of confusion.

My current site was developed by a third party - a Wordpress site using PHP. I want to replace this site with my ASP.NET Core2.0 web application.

The root of my current site is "httpdocs", where I have some subfolders with image files that I need for links from other applications. I'm not sure that they can remain as they are, or I need to move them to the new folder where the ASP.NET web application is located. I do not have access to the server directly, I can only access through cpanel.

The app requires https, and below I have included the Startup.cs and Program.cs files.

I have outlined the steps and considerations that, it seems to me, are involved in deployment and hosting. Can someone who has had previous experience with this help with my questions or let me know about something that I missed, or about other things that I should consider?

Startup.cs

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container public void ConfigureServices(IServiceCollection services) { try { services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => { options.ExpireTimeSpan = new TimeSpan(90, 0, 0, 0); options.LoginPath = new PathString("/Home/Index/"); options.AccessDeniedPath = new PathString("/Home/Index/"); options.LogoutPath = new PathString("/Home/Index/"); options.Validate(); }); services.AddMvc(); services.AddAntiforgery(); services.Configure<MvcOptions>(options => { options.Filters.Add(new RequireHttpsAttribute()); }); } catch (Exception ex) { gFunc.ProcessError(ex); } } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline // I've added a StaticFiles option that aims to make a directory called "SamadhiFiles" publically available // so that I can use "http://mysite.net.au/samadhifiles/myPic.png public void Configure(IApplicationBuilder app, IHostingEnvironment env) { try { app.UseMvc(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "SamadhiFiles")), RequestPath = "/SamadhiFiles" }); app.UseAuthentication(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); app.UseStatusCodePages(); } else { app.UseExceptionHandler("/Home/Error"); } // this code is to enable redirection of http to https var options = new RewriteOptions().AddRedirectToHttps(); app.UseRewriter(options); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } catch (Exception ex) { gFunc.ProcessError(ex); } } } 

Program.cs

 public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseIISIntegration() .UseKestrel() .Build(); } 

UseIISIntegration () tells ASP.NET that IIS will act as a reverse proxy before Kestrel. It also indicates some parameters that the Kestrel port should listen on, forward headers and other data. UseKestrel () registers the IServer interface for Kestrel as the server that will be used to host the application.

I have no doubt that I need to change something here or just use the deafult options.

I read: Microsoft recommends using IIS with any open site for the main ASP.NET host. IIS provides additional levels of configurability, management, security, logging, and more. One of the great benefits of using IIS is process control. IIS will automatically launch your application and possibly restart it if a failure occurs. If you used the ASP.NET Core application as a Windows application or a console application, you would not have such a security network to start and control the process for you.

launchSettings.json

Do I need to make changes to the contents of this file before publishing? Or does it automatically change when the application is published to a folder? For example, do I need to change ENVIRONMENT to "Production" or applicationUrl to my website domain?

 { "iisSettings": { "windowsAuthentication": true, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "https://localhost:44301/", "sslPort": 44376 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "CanvasWeb": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:61900/" } } } 

web.config (for ASP.NET core)

The web.config file should determine how IIS starts my ASP.NET Core process. For example, I want to enable output logging by setting stdoutLogEnabled = true, and I can also change the location of the log output as indicated by stdoutLogFile.

The IIS configuration is affected by the web.config section for those IIS features that apply to reverse proxy configuration.

My ASP.NET Core2 project currently does not have a "web.config" file. Will this file appear when publishing my application?

Publish to folder

When deploying from Visual Studio, the dotnet publishing step occurs automatically before the files are copied to the deployment destination.

The publication folder contains the .exe and .dll files for the application, its dependencies, and possibly .NET runtime. In addition to the .exe and .dll files, the publishing folder for the main ASP.NET application typically contains configuration files, static assets, and MVC views.

The .NET Core application can be published as a standalone or platform dependent application. If the application is self-contained, the .dll files that contain the .NET runtime are included in the publication folder. If the application is structure-dependent, the .NET runtime files are not included because the application has a link to the version of .NET installed on the server.

As I install for IIS on a Windows server, I believe that I should use the default deployment model, which is structure dependent.

Process manager

The ASP.NET Core application is a console application that should start when the server boots and restarts if it fails. A process manager is required to automate startup and restart. I know that I can use Apache for Linux, but in this case I need to use IIS, since my current site is a Windows server.

Reverse proxy setup

A scenario requiring a reverse proxy is when you have multiple applications that use the same IP port and port on the same server. This does not work directly with Kestrel, because Kestrel does not support sharing the same IP and port between multiple processes. When you configure Kestrel to listen on a port, it processes all traffic for that port regardless of the host header. A reverse proxy that can share ports should then forward Kestrel to a unique IP address and port.

According to MS, there are other reasons for using a reverse proxy. For example, it simplifies load balancing and SSL configuration. Your reverse proxy server requires an SSL certificate, and this server can communicate with application servers on the internal network using simple HTTP.

Install .NET Core Windows Server Hosting Bundle

I read that before deploying my application I need to install the .NET Core hosting package for IIS on the hosting. This installs the .NET Core runtime, libraries, and the ASP.NET kernel module for IIS. After installing it, you may need to "net stop was / y" and "net start w3svc" so that all changes are selected for IIS.

I can only access my web server through cpanel, so I think my hosting provider will need to do this.

SSL Certificate

Since I use https, I believe that I will need to purchase an SSL certificate and install it in IIS.

IIS Configuration - Creating an Application in IIS

I can only access my web server through cpanel, so I think it will also need to be done by my hosting provider.

  • Create a new IIS application pool. You will want to create it in the .NET CLR version β€œWithout Managed Code”. Since IIS only works as a reverse proxy, it does not actually execute any .NET code.
  • Create a new application on an existing IIS site or create a new IIS site. In any case, you will want to select your new IIS application pool and point it to the folder where you copied the ASP.NET output files for publication.
+8
c # asp.net-mvc iis
source share
1 answer

I looked through Nginx but could not get the necessary support, and I have zero experience with Linux.

Finally, I finally changed my hosting to a new provider (shared hosting on VPS), but it was still a nightmare that received good support.

As it turned out, I did not need to make any changes to the launchSettings.json file.

My web.config file ended as follows:

  <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\MyApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> </system.webServer> </configuration> <!--ProjectGuid: 4833e7c3-6fb8-4b3c-961f-f026b12af306--> 

My .csproj file looks like this:

  <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Aspose.Words" Version="18.1.0" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" /> <PackageReference Include="System.ServiceModel.Duplex" Version="4.4.0" /> <PackageReference Include="System.ServiceModel.Http" Version="4.4.0" /> <PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.0" /> <PackageReference Include="System.ServiceModel.Security" Version="4.4.0" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" /> </ItemGroup> <ItemGroup> <WCFMetadata Include="Connected Services" /> </ItemGroup> <ItemGroup> <Folder Include="wwwroot\CVFiles\" /> <Folder Include="wwwroot\icons\" /> <Folder Include="wwwroot\PictureFiles\" /> <Folder Include="wwwroot\resources\" /> </ItemGroup> </Project> 
0
source share

All Articles