2

Creating ASP.NET Core 6 MVC application with EF Core and Npgsql in Visual Studio 2022.

After clicking "Clear All NuGet Cache(s)" button in

Visual Studio > Tools Options > NuGet Package Manager > General

enter image description here

property Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9 in source code

CSharpParseOptions.Default.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9);

throws compile error

Error CS0117 'LanguageVersion' does not contain a definition for 'CSharp9'

All NuGet packages are up to date. How to fix this ? Assembly information does not contain CSharp9 member :

#region Assembly Microsoft.CodeAnalysis.CSharp, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// location unknown
// Decompiled with ICSharpCode.Decompiler 6.1.0.5902
#endregion

namespace Microsoft.CodeAnalysis.CSharp
{
    //
    // Summary:
    //     Specifies the language version.
    public enum LanguageVersion
    {
        //
        // Summary:
        //     C# language version 1
        CSharp1 = 1,
        //
        // Summary:
        //     C# language version 2
        CSharp2 = 2,
        //
        // Summary:
        //     C# language version 3
        //
        // Remarks:
        //     Features: LINQ.
        CSharp3 = 3,
        //
        // Summary:
        //     C# language version 4
        //
        // Remarks:
        //     Features: dynamic.
        CSharp4 = 4,
        //
        // Summary:
        //     C# language version 5
        //
        // Remarks:
        //     Features: async, caller info attributes.
        CSharp5 = 5,
        //
        // Summary:
        //     C# language version 6
        //
        // Remarks:
        //     Features:
        //     • Using of a static class
        //     • Exception filters
        //     • Await in catch/finally blocks
        //     • Auto-property initializers
        //     • Expression-bodied methods and properties
        //     • Null-propagating operator ?.
        //     • String interpolation
        //     • nameof operator
        //     • Dictionary initializer
        CSharp6 = 6,
        //
        // Summary:
        //     C# language version 7.0
        //
        // Remarks:
        //     Features:
        //     • Out variables
        //     • Pattern-matching
        //     • Tuples
        //     • Deconstruction
        //     • Discards
        //     • Local functions
        //     • Digit separators
        //     • Ref returns and locals
        //     • Generalized async return types
        //     • More expression-bodied members
        //     • Throw expressions
        CSharp7 = 7,
        //
        // Summary:
        //     C# language version 7.1
        //
        // Remarks:
        //     Features:
        //     • Async Main
        //     • Default literal
        //     • Inferred tuple element names
        //     • Pattern-matching with generics
        CSharp7_1 = 701,
        //
        // Summary:
        //     C# language version 7.2
        //
        // Remarks:
        //     Features:
        //     • Ref readonly
        //     • Ref and readonly structs
        //     • Ref extensions
        //     • Conditional ref operator
        //     • Private protected
        //     • Digit separators after base specifier
        //     • Non-trailing named arguments
        CSharp7_2 = 702,
        //
        // Summary:
        //     C# language version 7.3
        CSharp7_3 = 703,
        //
        // Summary:
        //     C# language version 8.0
        CSharp8 = 800,
        //
        // Summary:
        //     The latest major supported version.
        LatestMajor = 2147483645,
        //
        // Summary:
        //     Preview of the next language version.
        Preview = 2147483646,
        //
        // Summary:
        //     The latest supported version of the language.
        Latest = int.MaxValue,
        //
        // Summary:
        //     The default language version, which is the latest supported version.
        Default = 0
    }
}

Microsoft.CodeAnalysis.CSharp.dll file is not referenced directly from any anyprojet.

NuGet window does not show any package containing roslyn in name.

Andrus
  • 26,339
  • 60
  • 204
  • 378
  • Which language version does your project use? – PMF Dec 19 '21 at 15:30
  • And have you tried restarting VS? If your project supports it, try building from the command line first (with whatever you use for a command line build) – PMF Dec 19 '21 at 15:32
  • It looks like Ef Core Npgsql provider requires 9 since using 8 scaffold throws error `CS8400: Feature 'or pattern' is not available in C# 8.0. Please use language version 9.0 or greater.` After restarting Visual Studio, selecting clean project and rebuild all problem persists. How to build it from command line? – Andrus Dec 19 '21 at 21:03
  • Two things then: Make sure you also use language version 9 (or later) in your project. (set `9` in the project file). Most (larger) projects are prepared for building from the command line using some special build tools, such as Nuke. If you don't know what that is, you can try the standard .NET build command: `dotnet build `. – PMF Dec 19 '21 at 21:26
  • After building solution from command line problem persist. It looks that there are two versions of this dll file in solution. I updated question. How to remove older version ? – Andrus Dec 20 '21 at 19:39

1 Answers1

1

The error clearly indicates you are using an old version of the DLL that contains the enum, from before C#9 was made available.

Just upgrade the package in your projects to use the latest one and it should work fine. Here is the latest version of that package:

If upgrading manually is not possible, check for indirect dependencies on your project dependency tree, and upgrade whatever package is at the top that is referencing the old package. You can do that by navigating through the dependencies in the project's dependencies node in Solution Explorer.

Another way to find which project exactly is referencing the "problematic" (in this case, old) dll is by doing a windows search by the DLL name in the solution folder, and checking for the difference in file sizes: usually, the different versions will have different sizes, which then allows you to compare them across your projects and with the packages from NuGet.

julealgon
  • 7,072
  • 3
  • 32
  • 77
  • Solution does not contain any reference to this package. `Microsoft.CodeAnalysis.CSharp.dll` is only in bin\Debug\net6.0 folder in project. I bin and obj folders are deleted, rebuild loads this dll from unknown place and puts into `bin\Debug\net6.0` folders. How to upgrade this package? – Andrus Dec 20 '21 at 23:20
  • @Andrus this can be a bit tough, but you have to find "what package is bringing in this dependency": it is being installed transitively into your project. Once you update _that_ package, it should also update the transitive dependency. Alternatively, you can just explicitly install it in your failing projects and it should pick the version you install instead of the transitive one. That's probably easier, but not as clean. – julealgon Dec 21 '21 at 12:29
  • @Andrus Yet another place to check is by going into the Nuget Package Manager for the whole solution (right click solution -> Manage Nuget Packages for solution). Check for anything in the "Consolidate" tab (its best not to have anything in there ideally). Then, check for pending updates, see if there is some package in your solution that is massively out of date, and try updating those. Check for Razor-related dependencies, analyzer dependencies, anything that could potentially use CodeAnalysis.CSharp, and update those. – julealgon Dec 21 '21 at 12:43