Run multiple ASP.Net Core Projects using Dotnet Run Command

In this tutorial we are going to see how to run multiple ASP.Net Core projects using Dotnet CLI’s dotnet run command. Typically dotnet run command is used to run a specific .Net Core application from source code. But there might be scenarios where we want to run multiple applications simultaneously to complete the development of a particular business workflow. For example we might have one ASP.Net Core project for MVC and another for Web API (and MVC consuming Web API), in this scenario we have to run different command line interfaces (each specific to the project which is intended to run) with dotnet run commands. In the scope of this tutorial we are going to write a simple PowerShell script through which we will automate the entire process of running multiple ASP.Net Core Applications using dotnet run.

NOTE: Updated this tutorial on 2/26/2017 with MSBuild based Dotnet SDK.

I am using following version of SDK which is based on MSBuild/CSProj (remember that previous versions of ASP.Net Core are based on Project.json/XProj.).

NOTE: In VS 2017 (I am using RC4), we can setup multiple ASP.Net Core start up projects and run them simultaneously from VS itself.

image

Lets get started by creating multiple ASP.Net Core Projects . Below is my project structure.

image

WebApplication1 and WebApplication2 are ASP.Net Core Projects. Make sure to change the default hosting port for created ASP.Net Core Projects by following this tutorial –  JumpStart # 40 – Change default 5000 port of Dotnet Run command. I have configured WebApplication1 to be at 5001 and WebApplication2 at 5002 ports respectively.

Now create below PowerShell script and place it in the same same parent folder src (which is holding out web applications).

image

 <# Get current directory path #>
$src = (Get-Item -Path ".\" -Verbose).FullName;

<# Iterate all directories present in the current directory path #>
Get-ChildItem $src -directory | where {$_.PsIsContainer} | Select-Object -Property Name | ForEach-Object {
    $cdProjectDir = [string]::Format("cd /d {0}\{1}",$src, $_.Name);

    <# Get project's bundle config file path #>    
    $projectDir = [string]::Format("{0}\{1}\bundleconfig.json",$src, $_.Name); 
    $fileExists = Test-Path $projectDir;
    
    <# Check project having bundle config file #>
    if($fileExists -eq $true){
        <# Start cmd process and execute 'dotnet run' #>
        $params=@("/C"; $cdProjectDir; " && dotnet run"; )
        Start-Process -Verb runas "cmd.exe" $params;
    }
} 

Above script will first get the current working directory, iterate all the directories (typically in our case it is WebApplication1, WebApplication2) inside it. Then in each directory we check for the existence of Bundle Config file (I assume bundleconfig.json file will present in only Web Application projects). If bundleconfig.json file is present, then open a new command line interface and change directory to the project (which is under iteration). Finally execute the dotnet run command on the project.

Execute the above script in PowerShell console as show below. Make sure to change directory to the src before executing the script.

image

The result would be as shown below.

image

We can now browse respective URL’s to browse the applications. If we want to stop the execution, then we can simply do CTRL + C and stop execution.

Happy Coding and Stay Tuned!!!

You may also like...

  • John

    Nice article.