AspNetCore.Docs/aspnetcore/test/troubleshoot.md

7.5 KiB
Raw Blame History

title author description ms.author ms.custom ms.date uid
Troubleshoot ASP.NET Core projects Rick-Anderson Understand and troubleshoot warnings and errors with ASP.NET Core projects. riande mvc 11/26/2018 test/troubleshoot

Troubleshoot ASP.NET Core projects

By Rick Anderson

The following links provide troubleshooting guidance:

.NET Core SDK warnings

Both the 32 bit and 64 bit versions of the .NET Core SDK are installed

In the New Project dialog for ASP.NET Core, you may see the following warning:

Both 32 and 64 bit versions of the .NET Core SDK are installed. Only templates from the 64 bit version(s) installed at 'C:\Program Files\dotnet\sdk\' will be displayed.

A screenshot of the OneASP.NET dialog showing the warning message

This warning appears when both 32-bit (x86) and 64-bit (x64) versions of the .NET Core SDK are installed. Common reasons both versions may be installed include:

  • You originally downloaded the .NET Core SDK installer using a 32-bit machine but then copied it across and installed it on a 64-bit machine.
  • The 32-bit .NET Core SDK was installed by another application.
  • The wrong version was downloaded and installed.

Uninstall the 32-bit .NET Core SDK to prevent this warning. Uninstall from Control Panel > Programs and Features > Uninstall or change a program. If you understand why the warning occurs and its implications, you can ignore the warning.

The .NET Core SDK is installed in multiple locations

In the New Project dialog for ASP.NET Core, you may see the following warning:

The .NET Core SDK is installed in multiple locations. Only templates from the SDK(s) installed at 'C:\Program Files\dotnet\sdk\' will be displayed.

A screenshot of the OneASP.NET dialog showing the warning message

You see this message when you have at least one installation of the .NET Core SDK in a directory outside of C:\Program Files\dotnet\sdk\. Usually this happens when the .NET Core SDK has been deployed on a machine using copy/paste instead of the MSI installer.

Uninstall the 32-bit .NET Core SDK to prevent this warning. Uninstall from Control Panel > Programs and Features > Uninstall or change a program. If you understand why the warning occurs and its implications, you can ignore the warning.

No .NET Core SDKs were detected

In the New Project dialog for ASP.NET Core, you may see the following warning:

No .NET Core SDKs were detected, ensure they are included in the environment variable 'PATH'.

A screenshot of the OneASP.NET dialog showing the warning message

This warning appears when the environment variable PATH doesn't point to any .NET Core SDKs on the machine. To resolve this problem:

  • Install or verify the .NET Core SDK is installed.
  • Verify that the PATH environment variable points to the location in which the SDK is installed. The installer normally sets the PATH.

Obtain data from an app

If an app is capable of responding to requests, you can obtain the following data from the app using middleware:

  • Request Method, scheme, host, pathbase, path, query string, headers
  • Connection Remote IP address, remote port, local IP address, local port, client certificate
  • Identity Name, display name
  • Configuration settings
  • Environment variables

Place the following middleware code at the beginning of the Startup.Configure method's request processing pipeline. The environment is checked before the middleware is run to ensure that the code is only executed in the Development environment.

To obtain the environment, use either of the following approaches:

  • Inject the IHostingEnvironment into the Startup.Configure method and check the environment with the local variable. The following sample code demonstrates this approach.

  • Assign the environment to a property in the Startup class. Check the environment using the property (for example, if (Environment.IsDevelopment())).

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    IConfiguration config)
{
    if (env.IsDevelopment())
    {
        app.Run(async (context) =>
        {
            var sb = new StringBuilder();
            var nl = System.Environment.NewLine;
            var rule = string.Concat(nl, new string('-', 40), nl);
            var authSchemeProvider = app.ApplicationServices
                .GetRequiredService<IAuthenticationSchemeProvider>();

            sb.Append($"Request{rule}");
            sb.Append($"{DateTimeOffset.Now}{nl}");
            sb.Append($"{context.Request.Method} {context.Request.Path}{nl}");
            sb.Append($"Scheme: {context.Request.Scheme}{nl}");
            sb.Append($"Host: {context.Request.Headers["Host"]}{nl}");
            sb.Append($"PathBase: {context.Request.PathBase.Value}{nl}");
            sb.Append($"Path: {context.Request.Path.Value}{nl}");
            sb.Append($"Query: {context.Request.QueryString.Value}{nl}{nl}");

            sb.Append($"Connection{rule}");
            sb.Append($"RemoteIp: {context.Connection.RemoteIpAddress}{nl}");
            sb.Append($"RemotePort: {context.Connection.RemotePort}{nl}");
            sb.Append($"LocalIp: {context.Connection.LocalIpAddress}{nl}");
            sb.Append($"LocalPort: {context.Connection.LocalPort}{nl}");
            sb.Append($"ClientCert: {context.Connection.ClientCertificate}{nl}{nl}");

            sb.Append($"Identity{rule}");
            sb.Append($"User: {context.User.Identity.Name}{nl}");
            var scheme = await authSchemeProvider
                .GetSchemeAsync(IISDefaults.AuthenticationScheme);
            sb.Append($"DisplayName: {scheme?.DisplayName}{nl}{nl}");

            sb.Append($"Headers{rule}");
            foreach (var header in context.Request.Headers)
            {
                sb.Append($"{header.Key}: {header.Value}{nl}");
            }
            sb.Append(nl);

            sb.Append($"Websockets{rule}");
            if (context.Features.Get<IHttpUpgradeFeature>() != null)
            {
                sb.Append($"Status: Enabled{nl}{nl}");
            }
            else
            {
                sb.Append($"Status: Disabled{nl}{nl}");
            }

            sb.Append($"Configuration{rule}");
            foreach (var pair in config.AsEnumerable())
            {
                sb.Append($"{pair.Path}: {pair.Value}{nl}");
            }
            sb.Append(nl);

            sb.Append($"Environment Variables{rule}");
            var vars = System.Environment.GetEnvironmentVariables();
            foreach (var key in vars.Keys.Cast<string>().OrderBy(key => key, 
                StringComparer.OrdinalIgnoreCase))
            {
                var value = vars[key];
                sb.Append($"{key}: {value}{nl}");
            }

            context.Response.ContentType = "text/plain";
            await context.Response.WriteAsync(sb.ToString());
        });
    }
}