George Geschwend
George Geschwend

Reputation: 183

Having problems getting Console.WriteLine to work in a .NET Standard Library in Visual Studio 2017

Can you use a Console.Write in a .NET Standard Library project, with a Windows Console project as the "driver" (has a Main function that calls the library)? In other words, this is a library that I have in a solution where there is a Windows console application referencing the .NET Standard Library project, and it crumps when it hits the Console.WriteLine("Hello, World!"); statement.

The error I get is an System.IO.FileNotFoundException:

Could not load file or assembly 'System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

For the driver:

Target framework is: .NET 4.6.1
Output type is: Console

For the library:

Target is .NET Standard 4.1
Output type is: Class Library

So in main I am doing this:

   static void Main(string[] args)
   {
      TextToConsole textToConsole = new TextToConsole();
      textToConsole.GetTextFromSource();
      textToConsole.WriteToDestination(); //gets to here and barfs.

   }

The WriteToDestination method looks like this:

    public void WriteToDestination()
    {
        Console.WriteLine(textString);//It barfs here and throws an
                                      //unhandled exception.
        Console.ReadLine();
    }

Upvotes: 1

Views: 909

Answers (1)

George Geschwend
George Geschwend

Reputation: 183

OK. Stupid mistake. I had .NET Standard for the library's target framework originally, and that didn't match up to what I had for a target framework for the executable (driver); a .NET Framework. So for me, the .NET Standard library wasn't the right target framework.

I am guessing, that the library and the console application driver have to match, at least in this case. Visual Studio will not warn you if you mix target frameworks up. I changed the Library's target framework from .NET Standard, to .NET Framework. Now it works!

Should a console application that has a .NET Framework 4.6.1 referencing a library with .NET Standard 1.4 work? I don't know, but I would love to know.

Upvotes: 1

Related Questions