Search code examples
c#asp.netasp.net-corevisual-studio-codedotnet-cli

Creating and referencing class libraries with dotnet CLI and VSCode just like you can do in Visual Studio


I want to start using vscode to develop .net core apps, but I am a bit confused about how to create class libraries as separate projects and reference them.

For example: In a Visual Studio Solution I would add a Web API project and then several class libraries to that solution. Right click the Web API project and Add Reference as necessary.

Can this same thing be done with VS Code and dotnet CLI even though there is no solution concept?


Solution

  • Create Solution Folder

    c:\Projects>mkdir SampleDotNet
    c:\Projects>cd SampleDotNet
    

    Create SampleDotNet solution

    c:\Projects\SampleDotNet>dotnet new sln
    

    Create src folder (optional)

    c:\Projects\SampleDotNet>mkdir src 
    
    c:\Projects\SampleDotNet>cd src
    

    Create Web API Project

    c:\Projects\SampleDotNet\src>dotnet new webapi -n SampleDotNet.Api
    

    Create Class Library Project

    c:\Projects\SampleDotNet\src>dotnet new classlib -n SampleDotNet.Services
    

    Reference Library Project to Web API Project

    c:\Projects\SampleDotNet\src>dotnet add SampleDotNet.Api/SampleDotNet.Api.csproj reference SampleDotNet.Services/SampleDotNet.Services.csproj
    

    Finally Add Projects to Solution

    c:\Projects\SampleDotNet\src>cd ..
    c:\Projects\SampleDotNet>dotnet sln add src/SampleDotNet.Api/SampleDotNet.Api.csproj
    c:\Projects\SampleDotNet>dotnet sln add src/SampleDotNet.Services/SampleDotNet.Services.csproj
    

    Commands

    enter image description here

    Result

    Visual Studio Code

    enter image description here

    Visual Studio

    enter image description here