Scaffolding: Latest Version returns error: "Scaffolding failed. Failed to get Project Context"

Created on 11 Aug 2020  ·  20Comments  ·  Source: dotnet/Scaffolding

Steps to reproduce:

Once the ASP.NET MVC Core Web App has been created, run the following commands using the dotnet CLI:
dotnet tool install -g dotnet-aspnet-codegenerator
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet build
dotnet aspnet-codegenerator controller -name MoviesController -actions -api

Expected behavior:

This should create an API Controller with methods in it.

Actual behavior:

Scaffolding failed. Failed to get Project Context

Even when referencing options that do not exist, the error persists as "Failed to get Project Context". When uninstalling codegenerator and reinstalling version 3.1.3, the controller is created.

Additional information about the project being scaffolded, such as:

Target framework(s):

.NET Core 3.1

Package version of Microsoft.VisualStudio.Web.CodeGeneration.Design - this may be added to your project by scaffolding:

3.1.4

Most helpful comment

Hi All

I have the same problem :-(

dotnet --info

SDK for .NET Core (any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime:
 OS Name:     Mac OS X
 OS Version:  10.15
 OS Platform: Darwin
 RID:         osx.10.15-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.1.100/

Host (useful for support):
  Version: 3.1.0
  Commit:  65f04fb6db

.NET Core SDKs installed:
  3.1.100 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

dotnet tool list -g

dotnet-aspnet-codegenerator 3.1.4

I get this error even I run 'dotnet-aspnet-codegenerator' without any arguments:

$ codegen_trace=1 dotnet-aspnet-codegenerator

[Trace]: Command Line:
Scaffolding failed.
Failed to get Project Context for /Users/xyz/git/dotnet-test-web-api/TodoApi/TodoApi.csproj.

Usage: aspnet-codegenerator [arguments] [options]

Arguments:
  generator  Name of the generator. Check available generators below.

Options:
  -p|--project             Path to .csproj file in the project.
  -n|--nuget-package-dir
  -c|--configuration       Configuration for the project (Possible values: Debug/ Release)
  -tfm|--target-framework  Target Framework to use. (Short folder name of the tfm. eg. net46)
  -b|--build-base-path
  --no-build

[Trace]:    at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.GetProjectInformation(String projectPath, String configuration)
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.<>c__DisplayClass19_0.<Execute>b__0()
RunTime 00:00:00.97

my test project:

$ cat /Users/xyz/git/dotnet-test-web-api/TodoApi/TodoApi.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
  </ItemGroup>
</Project>

I repeated step by step from: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio-code

dotnet-aspnet-codegenerator --version 3.1.3 works for me!

All 20 comments

Hmm I cannot reproduce the debug. I'm assuming the build produced no errors. That is the only time that error message should be thrown.
Would you mind opening the project in VS(if you have it installed) and building it?

Wondering what dotnet sdk you are on? You can check using dotnet --info.

Hi All

I have the same problem :-(

dotnet --info

SDK for .NET Core (any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime:
 OS Name:     Mac OS X
 OS Version:  10.15
 OS Platform: Darwin
 RID:         osx.10.15-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.1.100/

Host (useful for support):
  Version: 3.1.0
  Commit:  65f04fb6db

.NET Core SDKs installed:
  3.1.100 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

dotnet tool list -g

dotnet-aspnet-codegenerator 3.1.4

I get this error even I run 'dotnet-aspnet-codegenerator' without any arguments:

$ codegen_trace=1 dotnet-aspnet-codegenerator

[Trace]: Command Line:
Scaffolding failed.
Failed to get Project Context for /Users/xyz/git/dotnet-test-web-api/TodoApi/TodoApi.csproj.

Usage: aspnet-codegenerator [arguments] [options]

Arguments:
  generator  Name of the generator. Check available generators below.

Options:
  -p|--project             Path to .csproj file in the project.
  -n|--nuget-package-dir
  -c|--configuration       Configuration for the project (Possible values: Debug/ Release)
  -tfm|--target-framework  Target Framework to use. (Short folder name of the tfm. eg. net46)
  -b|--build-base-path
  --no-build

[Trace]:    at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.GetProjectInformation(String projectPath, String configuration)
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.<>c__DisplayClass19_0.<Execute>b__0()
RunTime 00:00:00.97

my test project:

$ cat /Users/xyz/git/dotnet-test-web-api/TodoApi/TodoApi.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.7" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
  </ItemGroup>
</Project>

I repeated step by step from: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio-code

dotnet-aspnet-codegenerator --version 3.1.3 works for me!

I have the same problem too.

dotnet --info:

.NET Core SDK (反映任何 global.json):
Version: 3.1.100
Commit: cd82f021f4
執行階段環境:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.100\
Host (useful for support):
Version: 3.1.0
Commit: 65f04fb6db

.NET Core SDKs installed:
2.1.508 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download

===========================================

The .csproj file:

TargetFramework:netcoreapp3.1
PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7"
IncludeAssets: runtime; build; native; contentfiles; analyzers; buildtransitive
PrivateAssets: all
PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.7"
PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7"
PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4"

I also have the error "Failed to get Project Context for ..."
change dotnet-aspnet-codegenerator from 3.1.4 to 3.1.3 , then it works fine.

Hi,
Could you run a dotnet restore and dotnet build on the project successively. That exception should only pop up when a build/restore fails.

i am haveing the same issue

it started after i updated my system and the sdk

dotnet --info

.NET Core SDK (reflecting any global.json):
 Version:   3.1.401
 Commit:    39d17847db

Runtime Environment:
 OS Name:     linuxmint
 OS Version:  19.3
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/3.1.401/

Host (useful for support):
  Version: 3.1.7
  Commit:  fcfdef8d6b

.NET Core SDKs installed:
  3.1.401 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.7 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.7 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

dotnet tool list -g

Package Id                       Version      Commands                   
-------------------------------------------------------------------------
dotnet-aspnet-codegenerator      3.1.4        dotnet-aspnet-codegenerator
dotnet-ef                        3.1.5        dotnet-ef                  
dotnet-user-secrets              2.2.0        dotnet-user-secrets  

cat webistar.csprog

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>1234retfsdbdffsdfsfsdfsfsfB</UserSecretsId>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />
  </ItemGroup>

</Project>

I'm also having the same issue in my project. Scaffolding Failed , Failed to get Project Context

CoreWebAPI.csproj

`


netcoreapp3.1



runtime; build; native; contentfiles; analyzers; buildtransitive
all





`

I am too having the same issue.

dotnet aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers
Scaffolding failed.
Failed to get Project Context for C:\Users\damithk\projects\dotnet-core\TodoApi\TodoApi.csproj.

at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.GetProjectInformation(String projectPath, String configuration) in /_/src/dotnet-aspnet-codegenerator/Program.cs:line 303
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.BuildAndDispatchDependencyCommand(String[] args, String projectPath, String buildBasePath, String configuration, Boolean noBuild, ILogger logger) in /_/src/dotnet-aspnet-codegenerator/Program.cs:line 175
at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.<>c__DisplayClass19_0.b__0() in /_/src/dotnet-aspnet-codegenerator/Program.cs:line 131
RunTime 00:00:03.23

I also have the error "Failed to get Project Context for ..."
I ran into the same issue after a successful restore and build of the project.
Downgrading aspnet-codegenerator to 3.1.3 and it works.

Same error here. I am following this tutorial by Microsoft: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio-code#prerequisites-1

My build is working fine but not the code Scaffolding using this command
dotnet aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers

Scaffolding failed.
Failed to get Project Context for /Users/......./TodoApi/TodoApi.csproj.

[Trace]:    at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.GetProjectInformation(String projectPath, String configuration)
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.BuildAndDispatchDependencyCommand(String[] args, String projectPath, String buildBasePath, String configuration, Boolean noBuild, ILogger logger)
   at Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program.<>c__DisplayClass19_0.<Execute>b__0()
RunTime 00:00:01.13

Some people have suggested downgrading the dotnet-aspnet-codegenerator to 3.1.3, even that did not help. I have tried using 3.1.0 too.

Below is my dotnet setup.

.NET Core SDK (reflecting any global.json):
 Version:   3.1.402
 Commit:    9b5de826fd

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.15
 OS Platform: Darwin
 RID:         osx.10.15-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.1.402/

Host (useful for support):
  Version: 3.1.8
  Commit:  9c1330dedd

.NET Core SDKs installed:
  2.2.207 [/usr/local/share/dotnet/sdk]
  3.1.402 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

My csproj file

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.8" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
  </ItemGroup>

</Project>

I also have this same issue. I have been following this tutorial but I have run up against this same error, and downgrading to scaffolder 3.1.3 fixed my error on MacOS.

Dotnet version: 3.1.201

Have this issue as well. I suspect this might be an issue on Linux/MacOS, because the project itself builds and runs just fine. Testing it on Windows/VS everything is fine as well.

Same problem here (Windows 10), downgrading to 3.1.3 solved it.

Linux, still happened on 3.1.3 until I built the project first.

I had this issue and I managed to get it to work. The project woudl build but it complained about missing references as warnings. I had deleted some projects that were still references in files. This did not stop it from building successfully though, it just created warnings. After removing these references (in the .csproj files) and building without missing reference warnings, I was able to scaffold.

Note however that it still requires a downgrade to 3.1.0

i'm experiencing the issue but it seems to be resolved when i run dotnet build before scaffolding

Just build your project first before scaffolding, if it can build then the scaffolding should be fine, otherwise this will continue appear.

Hi,
Could you run a _$dotnet restore_ and _dotnet build_ on the project successively. That exception should only pop up when a build/restore fails.

Doing this fixed the issue for me

i have the same problem after i updated to 3.1.10 the error was gone.

Had the same problem. For me after removing Microsoft.VisualStudio.Web.CodeGeneration.Tools worked

Was this page helpful?
0 / 5 - 0 ratings