dotnet / Scaffolding

Code generators to speed up development.
MIT License
632 stars 226 forks source link

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

Open JacquiM opened 4 years ago

JacquiM commented 4 years ago

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

deepchoudhery commented 4 years ago

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.

sokovykh commented 4 years ago

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!

pintse commented 4 years ago

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"

theezz commented 4 years ago

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.

deepchoudhery commented 4 years ago

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.

JoeSherwood335 commented 4 years ago

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>
Suchitech02 commented 3 years ago

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

`

Damith88 commented 3 years ago

I am too having the same issue.

dotnet aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers [Trace]: Command Line: 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.

[Trace]: at Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build() in //src/Ext.ProjectModel.MsBuild.Sources/MsBuildProjectContextBuilder.cs:line 56 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

wchesley commented 3 years ago

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.

maruthimohan commented 3 years ago

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>
zach-delong commented 3 years ago

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

bioan commented 3 years ago

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.

sorin-costea commented 3 years ago

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

ccaneke commented 3 years ago

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

RenegadeMaster commented 3 years ago

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

qin-guan commented 3 years ago

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

klauszou commented 3 years ago

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

psmatsinhe commented 3 years ago

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

longcharmroeun commented 3 years ago

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

long-blade commented 3 years ago

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

EtienneBel commented 2 years ago

Had the same problem when run

dotnet aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers.

i solve this by unsinstalling dotnet aspnet-codegenerator controller

dotnet tool uninstall -g dotnet-aspnet-codegenerator

and install a stable version with my sdk version (check it with dotnet --info)

dotnet tool install -g dotnet-aspnet-codegenerator --version 6

and it's work for me

ferhatsal commented 2 years ago

Same issue , and solved by uninstalling dotnet aspnet-codegenerator controller and installing the version 6 as the @EtienneBel suggested. Thanks bro.

Jie1002 commented 2 years ago

same issue, and solved by uninstalling dotnet aspnet-codegenerator controller and installing version 6 as @EtienneBel suggested. Thank you.

girishgodage commented 2 years ago

Same issue , and solved by uninstalling dotnet aspnet-codegenerator controller and installing the version 6 as the @EtienneBel suggested. Thanks You

EugeneKim commented 2 years ago

I have the dotnet 5.0.1 and 6.0.1 installed on my pc, and downgrading dotnet-aspnet-codegenerator to 3.1.3 didn't work.

@EtienneBel's suggestion fixed the issue.

Microsoft should update the doc to use the stable version not 6.0.0-preview.7.21413.1. https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio-code

goerkemballmueller commented 2 years ago

@EtienneBel's suggestion works. Thanks

barcollin commented 2 years ago

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.

Works like magic

sandovro commented 2 years ago

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.

Thank you! That did it for me

Gildedter commented 2 years ago

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.

Didn't work for me, my project structure is based on clean architecture, src folder containing Domain (where my entities are), Infrastructure (where my dbcontext and entity configurations are) and (ASP .NET Core SPA template) WebUI (where I'm trying to generate my api controllers)

mikehengetech commented 1 year ago

Solution that worked for me: install this: Microsoft.VisualStudio.Web.CodeGeneration.Utils v6.0.10 for ( Microsoft.VisualStudio.Web.CodeGeneration.Design v6.0.10) (version could be different v6.0.10) just like here: https://www.youtube.com/watch?v=46mZCEHgBK4

derek-mba commented 1 year ago

Solution that worked for me: install this: Microsoft.VisualStudio.Web.CodeGeneration.Utils v6.0.10

That's odd. Nuget says there's no Microsoft.VisualStudio.Web.CodeGeneration.Utils in any version