Search code examples
c#visual-studionamespaces.net-6.0

RootNamespace not being used as namespace prefix


I recently created a new .net6 project and moved my source code into it. I've set the following in the project file

<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>

In the project properties there is a section at the bottom of Application -> General there is a "Default namespace" which also has "$(MSBuildProjectName.Replace(" ", "_"))"

For the sake of this question I have omitted the solution completely and let's assume my project structure is as follows

MyProject (this is a project with project file MyProject.csproj)

  • DB (this is a folder in the project)
    • MySQL (this is a sub-folder under the DB folder)
    • MSSQL (this is a sub-folder under the DB folder)
  • UI (this is a folder in the project)

If I add a new class at the root level of my project it's namespace is MyProject however if I add a new class to DB it's namespace is DB.

I expect this to be MyProject.DB

Similarly if I add a new class to the MSSQL project I get a namespace of DB.MSSQL whereas I expect this to be MyProject.DB.MSSQL

I have tried hard coding the $(MSBuildProjectName.Replace(" ", "_")) as MyProject but this doesn't help.


Solution

  • As strange as this sounds, the problem was with WebView2 version 1.0.1343.22 Updating to version 1.0.1370.28 resolved the issue

    I did also recreate this in a fresh solution with only 1 project and no other dependencies to ensure that is the case.

    Interestingly the default using statements are also different.

    With version 1.0.1343.22 installed, creating a new class file you get

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace DB.MsSQL
    {
        internal class Class1
        {
        }
    }
    

    Whereas with version 1.0.1370.28 a new class file looks like this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WebView2NamespaceIssue.DB.MsSQL
    {
        internal class Class2
        {
        }
    }
    

    Both of these classes were created in the same folder of the same project. The only difference was changing the version of WebView2 being referenced