4

I created a .NET Framework 4.8 Console application in Visual Studio 2019, to which I added the nuget package Microsoft.Extensions.Hosting, that means, in the .csproj file I added the line:

<PackageReference Include="Microsoft.Extensions.Hosting" />

Program.cs looks as follows:

using System;
using Microsoft.Extensions.Hosting;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IHost host = Host.CreateDefaultBuilder(args).Build())
            {
                Console.WriteLine("ok");
                Console.ReadKey();
            }
        }
    }
}

When I compile, I get the error:

1>C:\temp\ConsoleApp4\Program.cs(22,33,22,37): error CS0103: The name 'Host' does not exist in the current context

However the Host class is in the namespace Microsoft.Extensions.Hosting.

  1. How can I make this work?
  2. Which is the correct way to use dependency injection in .NET Framework 4.8 Console application?
Alex
  • 419
  • 1
  • 5
  • 14

0 Answers0