Null reference exception when creating a WebHost with Microsoft.Extensions.Configuration v.5.0.0

-2

I took a very simple example of setting up a webhost from microsoft docs ASP.NET Core Web Host:

class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("hostsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();
     return WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:5000")
            .UseConfiguration(config)
            .Configure(app =>
            {
                app.Run(context =>
                    context.Response.WriteAsync("Hello, World!"));
            });
    }
}

And when I run and go in a browser "http://localhost:5000/", the line "context.Response.WriteAsync("Hello, World!"));" throws null pointer exception:

> System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.AspNetCore.Server.Kestrel.Core
StackTrace:
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.CreateResponseHeader(Boolean appCompleted)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProduceStart(Boolean appCompleted)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.InitializeResponseAsync(Int32 firstWriteByteCount)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WriteAsync(ReadOnlyMemory`1 data, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, Encoding encoding, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, CancellationToken cancellationToken)
at WebApp.Program.<>c.<CreateWebHostBuilder>b__1_1(HttpContext context) in C:\Users\AndriyLisovoy\source\repos\WebApp\Program.cs:line 32

It works perfectly if I change the version of "Microsoft.Extensions.Configuration" to 3.1.12 in the project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
    <!--<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />-->
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.12" />
  </ItemGroup>

</Project>    

Does anybody know how can I make it working with the latest version of the library? thank you!

c#
microsoft.extensions.configuration
asked on Stack Overflow Feb 16, 2021 by Andriy Lisovoy • edited Feb 16, 2021 by Pavel Anikhouski

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0