Winforms: .NET Core 3.1 console apps with UseWindowsForms set to true in .csproj always fails since .NET 5.0

Created on 17 Nov 2020  路  5Comments  路  Source: dotnet/winforms

Description

If I create this simple .NET Core 3.1 Console project (on Windows, Visual Studio or not), it works fine:

Project.csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>

Program.cs:

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    var key = Console.ReadKey(true);
}

Now, if I add this to the .csproj in the PropertyGroup Xml element:

<UseWindowsForms>true</UseWindowsForms>

Eveything compiles fine, but I get this error everytime on the ReadKey() call:

System.InvalidOperationException: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

Regression

This worked. I think it fails since I installed the brand new released .NET 5.0, through Visual Studio 2019 updates. (I though all this was side by side?).

Other information

The workaround is to call Win32's AllocConsole "manually" once at startup. But it requires a change and recompilation of all .NET core 3.1 console apps.

Most helpful comment

I found the fix. Just add this

<DisableWinExeOutputInference>true</DisableWinExeOutputInference>

To the .csproj. It's documented here: https://docs.microsoft.com/en-us/dotnet/core/compatibility/winforms#outputtype-set-to-winexe-for-wpf-and-winforms-apps (not easy to find...)

All 5 comments

Tagging subscribers to this area: @eiriktsarpalis, @jeffhandley
See info in area-owners.md if you want to be subscribed.


Issue Details





















Description:

Description

If I create this simple .NET Core 3.1 Console project (on Windows, Visual Studio or not), it works fine:

Project.csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>

Program.cs:

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    var key = Console.ReadKey(true);
}

Now, if I add this to the .csproj in the PropertyGroup Xml element:

<UseWindowsForms>true</UseWindowsForms>

Eveything compiles fine, but I get this error everytime on the ReadKey() call:

System.InvalidOperationException: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

Regression

This worked. I think it fails since I installed the brand new released .NET 5.0, through Visual Studio 2019 updates. (I though all this was side by side?).

Other information

The workaround is to call Win32's AllocConsole "manually" once at startup. But it requires a change and recompilation of all .NET core 3.1 console apps.

Author: smourier
Assignees: -
Labels:

area-System.Console, untriaged

Milestone: -

This is not specific to .NET Core 3.1, it also reproduces in 5. I'm not quite the WinForms expert, but this seems like by-design behaviour: stdin is redirected for the process, and therefore calling Console.ReadKey() will fail.

I said nothing about .NET 5 because I wasn't using the preview versions, but as is, yes it behaves the same.

It breaks existing .NET Core 3.1 projects at execution time and I don't see why it would be by design since it used to work fine until very recently. It looks like a regression instead.

You're right that this behaviour does not reproduce in netcoreapp2.0. Honestly I don't know why, but it might make sense to transfer this issue to the dotnet/winforms repo.

I found the fix. Just add this

<DisableWinExeOutputInference>true</DisableWinExeOutputInference>

To the .csproj. It's documented here: https://docs.microsoft.com/en-us/dotnet/core/compatibility/winforms#outputtype-set-to-winexe-for-wpf-and-winforms-apps (not easy to find...)

Was this page helpful?
0 / 5 - 0 ratings