Console.BufferHeight in netcoreapp2.1 raise Exception PlatformNotSupportedException in Ubuntu

88 views Asked by At

I have a Console application build in dotnetcorapp2.1. It's running fine on Windows.

When trying to run on Ubuntu it raise an Exception error:

System.PlatformNotSupportedException: Operation is not supported on this platform. at System.ConsolePal.set_BufferHeight(Int32 value)

That is because This line:

     Console.BufferHeight = Int16.MaxValue - 1;

This document didn't mention the limitation that I take into account for Console.BufferHeight

Googling didn't help like this and this

What is the limitation of Console.BufferHeight (and BufferWidth) on Ubuntu?

1

There are 1 answers

0
M.Hassan On

I have posted this question to corefx team and submitted an issue there.

All credit in this answer go to @danmosemsft

The member Console.BufferHeight is not supported on Unix. The docs didn't state the Unix specific behaviors on the Console methods/properties.

The class ConsolePal in the document ConsolePal.Unix.cs include also other NotSupported members.

A new document is created to show the PlatformNotSupportedExceptions in System.Console

I modified the code and catch PlatformNotSupportedException separate and application is running on Ubuntu without a problem.

      try
      {
         //.... do your stuff here
      }
      catch (PlatformNotSupportedException )
      {
        //log PlatformNotSupportedException 
        //noop
      }
      catch (exception ex)
      {

         //handle exception
      }

Edit

Discovering the code that raise PlatformNotSupportedExceptions:

  • Install the nuget package Microsoft.DotNet.Analyzers.Compatibility

    Install-Package Microsoft.DotNet.Analyzers.Compatibility -Version 0.2.12-alpha
    
  • Configure the tool as described here

  • Select the rule PC001 and select Set Rule Set Severity as error

  • When building the project, any issues related to PlatformNotSupportedExceptions is displayed as error in Error window