IISIntegration icon indicating copy to clipboard operation
IISIntegration copied to clipboard

asp net core 2.0 hosting on IIS returns blank page with error 500

Open obstler42 opened this issue 9 years ago • 23 comments

I'm trying to deply an ASP NET Core 2.0 full framework app to IIS in Windows Server 2012 R2 and keep on failing. The app runs fine locally in VS2017 and also works on the server when I manually run the EXE and connect to the kestrel server on localhost:5000. However trying to host in IIS always results in an error 500 from IIS.

I've followed the instructions on https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x but I don't see any mentions of setting up for asp net core 2 with full framework (instead of net core). the app uses EF6, so it targets full .net framework 461. the .NET Core Windows Server Hosting bundle is installed, and server rebooted afterwards. there are also other standard asp.net websites running on the server. the asp net core iis site is configured according to instructions with app pool set to "No managed code".

the web.config is

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\appng.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
        <httpErrors errorMode="Detailed" />
  </system.webServer>
</configuration>
<!--ProjectGuid: c458df4b-f145-427d-9bfb-5959b2a0932b-->

enabling the stdout logging in web.config shows the following error: "Unrecognized configuration section runtime." stdout log:

Hosting environment: Production
Content root path: C:\inetpub\appng
Now listening on: http://localhost:17402
Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HL7V7O2GO450", Request id "0HL7V7O2GO450:00000001": An unhandled exception was thrown by the application.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section runtime. (C:\inetpub\appng\appng.exe.Config line 3)

the contents of appng.exe.config is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.Serialization.Xml" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.3.0" newVersion="4.1.3.0" />
      </dependentAssembly>
    </assemblyBinding>
... [lots more assemblybindings]
  </runtime>
</configuration>

this is where I've tried for searching any errors related to that but simply cannot find anything.

the app runs fine in vs2017 and also on a test server with iis on server 2008r2, installed the same way as on the 2012r2 where it doesn't work.

any help will be greatly appreciated, as I need to get it working on our web server.

obstler42 avatar Sep 22 '17 06:09 obstler42

Does the folder path exist for .\logs\stdout?

jkotalik avatar Sep 22 '17 06:09 jkotalik

yes -- the stdout log is where the error ""Unrecognized configuration section runtime." is logged

obstler42 avatar Sep 22 '17 06:09 obstler42

Ahh yeah my bad, you did mention that. The file not existing is a super common issue. I believe this should work. Just to confirm, the same application when run on server 2008r2 environment worked?

I can't see why the section would be invalid in configuration. @Tratcher thoughts?

jkotalik avatar Sep 22 '17 07:09 jkotalik

yeah, I installed it on a 2008r2 test server in exactly the same way and it works in IIS there.

obstler42 avatar Sep 22 '17 07:09 obstler42

I haven't seen this. Here are a few things to try:

  • Change the app pool user identity to a normal user like yourself.
  • Set the app pool to "No managed code"
  • Compare version of .NET that your app targets to the one installed on the server. The serer should have the same or higher version.

Tratcher avatar Sep 22 '17 17:09 Tratcher

Tried changing app pool user identity to my user, same result as when it is set to "ApplicationPoolIdentity"

app pool is set to "No managed code"

The server has .net 4.7 installed, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\Release" is "460805"

The app targets net461 runtime win7-x86 (only option available in the publish profile of the aspcore2 template in vs2017). works fine on the dev win10x64 machine and on a test server 2008r2 x64.

obstler42 avatar Sep 22 '17 17:09 obstler42

maybe you wanna share how you config your program.cs and startup.cs. If you config it incorrectly, it might lead to 500 error as well but run fine on localhost. For example appsettings.json doesn't get copied to the server.

maxisam avatar Oct 20 '17 19:10 maxisam

the exact same app / config (the whole folder copied to the other systems) works just fine on two other, systems -- a server2008r2, and a new test VM with server 2016; so this seems to be something machine specific, probably some .NET issue.

the three files you asked for are below, in case it helps to figure out the issue...

appsettings.json:

{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "ConnectionStrings": { "removed for posting here...": "..." } } programs.cs: `using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging;

namespace wkotvng { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

} `

startup.cs: `using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using vDB.Models; using Microsoft.AspNetCore.Rewrite; using System.Net; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.EntityFrameworkCore; using vng.Models;

namespace wkotvng { 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)
	{
		services.AddScoped<vContext>(_ => new vContext(Configuration.GetConnectionString("dbstring")));
		services.AddAuthentication(options =>
		{
			options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
			options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
			options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
		}).AddCookie();

		services.AddMvc();

	    services.AddDbContext<vngContext>(options =>
	            options.UseSqlServer(Configuration.GetConnectionString("vngContext")));
	}

	// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
	public void Configure(IApplicationBuilder app, IHostingEnvironment env)
	{
		if (env.IsDevelopment())
		{
			app.UseDeveloperExceptionPage();
			app.UseBrowserLink();
		}
		else
		{
			app.UseExceptionHandler("/Error");
		}


		app.UseStaticFiles();

		app.UseAuthentication();

		app.UseMvc(routes =>
		{
			routes.MapRoute(
				name: "default",
				template: "{controller}/{action=Index}/{id?}");
		});
	}
}

} `

obstler42 avatar Oct 20 '17 20:10 obstler42

Seems like it.

Another easy way to make sure the server setting is correct is to generate a testing project straight from a template and deploy it. If it doesn't work, you have a server issue.

maxisam avatar Oct 20 '17 20:10 maxisam

Ever find a solution to this issue? I am having the same problem. DeveloperExceptionPage is working, but outside of a development environment I am getting blank pages for 500 error

arufolo avatar Nov 07 '17 17:11 arufolo

I'm having the same issue on Windows 7, IIS 7.5 with ASP.Net Core Hosting bundle 2.0.5. Any solutions?

enginaar avatar Jan 17 '18 22:01 enginaar

The solution is to install Microsoft Visual C++ 2015 Redistributable Update 3 on the server. It can be downloaded from: https://www.microsoft.com/en-us/download/details.aspx?id=53840

Somehow, the Windows Server Hosting fails to install this. If you have already installed dot net core windows server hosting, you may need to reinstall after install Visual C++ update.

narind3rs avatar Feb 01 '18 21:02 narind3rs

Hi I think you have to copy "Views" folder to the published project folder aside with WWWroot folder don't forget appsetting.json in general if you stuck at any point you can use the following code at Program.cs

WebHost.CreateDefaultBuilder(args) //.UseKestrel(options => { // options.Listen(IPAddress.Loopback, 62145); //}) .UseKestrel() .UseApplicationInsights() .UseSetting(WebHostDefaults.DetailedErrorsKey, "true") // .UseEnvironment("Development") --> this will let you see the error rather than a blank page //.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "assembly1;assembly2") .PreferHostingUrls(false) //.UseSetting(WebHostDefaults.PreventHostingStartupKey, "true") // .UseShutdownTimeout(TimeSpan.FromSeconds(10)) //.UseWebRoot("public") //.UseStartup("MIS.WEBUI") .UseIISIntegration() .UseStartup() .Build();

mfbasha avatar Feb 04 '18 20:02 mfbasha

@narind3rs reinstalled it even though there was a new version already installed. It didn't make any difference. Actually, Microsoft Visual C++ 2017 removes 2015 when installing and you cannot install 2015 if 2017 is already installed so, I'm not sure what changed.

@mfbasha I'm using error logging, thanks. UseIISIntegration is also being called, I'm not seeing anything else in your builder that could have any relation to IHttpContextAccessor.

Error message I'm seeing in the log files is

System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' has been registered.

enginaar avatar Feb 05 '18 19:02 enginaar

Same files on Windows Server 2012 with IIS 8.5 and there is no error message logged anymore, nothing in event viewer either. I even went ahead and granted everyone all the permission to write to website folder with no luck of figuring out why it's not logging anymore.

Am I making a huge mistake using .net core for production?

enginaar avatar Feb 05 '18 22:02 enginaar

@enginaar Same issue, same question...

zoamer avatar Feb 06 '18 08:02 zoamer

@zoamer

I found the solution in adding a singleton instance of HttpContextAccessor to services in ConfigureServices method of Startup class. Strangely this is not needed when debugging.

public class Startup {
  ...
  public virtual void ConfigureServices(IServiceCollection services) {
    ...
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    ...
  }
  ...
}

enginaar avatar Feb 06 '18 15:02 enginaar

@enginaar Thanks for reply!

Somehow i found out that this line of code was already existing in our solution, as this is also needed for the NLog.Web NuGet package.

For me the application was also running fine in debugger. On server after publishing i got 500 error. Found out that the dotnet run command was working fine in the console, without any errors... So i updated Node.js on both build agent and Web App Host from version 8.1.3 to 8.9.4 and hooray....App was running fine both in debugger and on Server.

On Asp Net 4.X we did't have this kind of problems... But performance of Asp Net Core is really good!!

zoamer avatar Feb 07 '18 06:02 zoamer

@pan-wang @shirhatti

muratg avatar Feb 12 '18 22:02 muratg

Similar issue with Windows Server 2012 R2, the default template published app is running fine on Linux (Ubuntu 18.04), My local Win 10 (when running via IIS), but getting 503 error in server 2012R2. The App-Pool created (with No Managed Code) keeps getting stop as soon as I hit the url (locathost:8080).

It's very frustrating to host AspNetCore 2.1 on server 2012.

In fact it is something which can be easily replicated.

Note:

  1. I am deploying self contained app.
  2. Even I tried on different Server 2012 R2 machine (got some different issues).

koder101 avatar Jul 06 '18 07:07 koder101