Getting help on dotnet-aspnet-codegenerator debug output - asp.net-core

I am scaffolding a local login identity page in into a asp.net core solution that already has default identity. I am doing this in order to customize my login page.
I have successfully done this using two test apps which use the same base code with progressively more packages installed) before attempting this on my final.
For my app, when I issue the following command, I'm getting this error:
PM> dotnet-aspnet-codegenerator identity --dbContext ApplicationDbContext
Failed to get Project Context for C:\Users\...\rollbase.csproj.
To see more information, enable tracing by setting environment variable 'codegen_trace' = 1
There is no information about 'codegen_trace' in docs.
I have searched and found a lot of references to this error. But can't find anything for:
What is the 'Project Context'
How do I use a 'codegen_trace'
The solution builds successfully.
I have been very careful keeping track of the packages involved
Microsoft.VisualStudio.Web.CodeGeneration.Design
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Identity.UI
Azure.Identity
Using
<TargetFramework>net5.0</TargetFramework>
dotnet-aspnet-codegenerator' (version '5.0.0')
dotnet sdk 5.0.403
Microsoft.NETCore.App 5.0.12
Most posts talk of rolling back the tool version/packages etc. I was wanting to know if anyone out there can actually point to a solid debug strategy.
I am adding info to this question below
Using help provided - output from codegen_trace
Microsoft.Extensions.ProjectModel.MsBuildProjectContextBuilder.Build()
/_/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 290
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 173
Microsoft.VisualStudio.Web.CodeGeneration.Tools.Program
.<>c__DisplayClass19_0.<Execute>b__0() in
/_/src/dotnet-aspnet-codegenerator/Program.cs:line 129
From this output it looks like a problem in the installed package Microsoft.VisualStudio.Web.CodeGeneration.Tools 5.0.2
I have tried to update the package to 6.0,0 in Visual Studio 2019 (latest) but get the following:
NU1202: Package Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.0 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.0 supports: net6.0 (.NETCoreApp,Version=v6.0)
I looked at the github repo for this project but could not find any documentation regarding compatibility with .NETCoreApp,Version=v5.0
With reference to the original error message:
PM> dotnet-aspnet-codegenerator identity --dbContext ApplicationDbContext
Failed to get Project Context for C:\Users\...\rollbase.csproj
I have compared the .csproj files for the project where codegenerator works against the one where codegenerator fails to get Project Context.
The main difference is in the Property Group.
The codegnerator works:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>aspnet-testbase1-57C4221E-46E9-4653-A734-4C412F7C523D</UserSecretsId>
</PropertyGroup>
Where Project Context not found:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>aspnet-rollbase-6D7D2449-EB25-45CE-A8D3-EDAB69CDCAB3</UserSecretsId>
<RootNamespace></RootNamespace>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<AssemblyName />
<PackageId />
<EnableNETAnalyzers>false</EnableNETAnalyzers>
</PropertyGroup>

The best way to understanding the codegen_trace environment variable and it's output is reading the source code.
To enable the Trace logs, set the codegen_trace environment variable before you run dotnet-aspnet-codegenerator:
Command Prompt
SET codegen_trace=1
PowerShell
$env:codegen_trace=1
Bash/Zsh/Fish
export codegen_trace=1
The Project Context means the context of the project information. Most of the project information included in *.csproj file. Your problem is very possible that your project can't build successfully. You have to make sure your project is buildable.
For your dotnet-aspnet-codegenerator identity --dbContext ApplicationDbContext command, you at least need to install the following packages to get things right.
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.AspNetCore.Identity.UI
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design

Please check the version of your code generator CLI tool version is the same as with your.NET version. You need to set the codegen_trace as an Environmental variable like this if you're in Linux or mac
codegen_trace=1
If you're using Powershell you can set it like this.
$env:codegen_trace=1
There is a GitHub open issue related to this. Please check the solutions on that as well. - https://github.com/dotnet/Scaffolding/issues/1388

I edited the project file PropertyGroup section. From what it was (see additional info in question above) to:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>aspnet-rollbase-6D7D2449-EB25-45CE-A8D3-EDAB69CDCAB3</UserSecretsId>
</PropertyGroup>
Identity files were scaffolded using:
dotnet-aspnet-codegenerator identity --dbContext ApplicationDbContext
[Trace]: Command Line: identity --dbContext ApplicationDbContext
Building project ...
[Trace]: Command Line: --no-dispatch --port-number 52819 identity --dbContext ApplicationDbContext --dispatcher-version 5.0.0+a93dad81ee7f820d8e33d3f91e066ef68053d004
Finding the generator 'identity'...
Running the generator 'identity'...
...etc...

Related

how to use Microsoft.VisualStudio.Web.CodeGeneration?

I find that The package:Microsoft.VisualStudio.CodeGeneration has over 20,000,000 downloads in the nuget.but I can not find any doc.
how to use it?
any articles about it?
What does this package do
Basically, the package offers a single command to generate code:
dotnet aspnet-codegenerator {name}
You can find the source code here.
How to use the package
We don't use the Microsoft.VisualStudio.Web.CodeGeneration directly unless we're creating a new command to generate code.
Because it is a command library for generic purpose, concrete commands are defined in other packages. For example, the dotnet aspnet-codegenerator controller command is defined in Microsoft.VisualStudio.Web.CodeGenerators.Mvc. And the command dotnet aspnet-codegenerator identity is also defined in the CG.MVC package.
Usually , since this package is a generic purpose library, you won't reference this package directly. Instead, You'll add the package Microsoft.VisualStudio.Web.CodeGeneration.Design. Be aware the Microsoft.VisualStudio.Web.CodeGeneration.Design package has a dependency on Microsoft.VisualStudio.Web.CodeGenerators.Mvc, and the Microsoft.VisualStudio.Web.CodeGenerators.Mvc depends on Microsoft.VisualStudio.Web.CodeGeneration:
Microsoft.VisualStudio.Web.CodeGeneration.Design
|
|(depends on)
|-----------> Microsoft.VisualStudio.Web.CodeGenerators.Mvc
|
|(depends on)
|-----------> Microsoft.VisualStudio.Web.CodeGeneration
Be aware the Microsoft.VisualStudio.Web.CodeGeneration.Design is automatically added into your dependencies when you use Visual Studio to scaffold a controller/identity.
If you're using a VSCode/CLI, you need to manually add such a package reference. See https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-3.0&tabs=visual-studio-code#add-nuget-packages
What #itminus says. I think the reason it has so many downloads is from the clear error message when you try to use dotnet aspnet-codegenerator without Microsoft.VisualStudio.Web.CodeGeneration.Design NuGet.
As of 2021-08-27 it has 90,672,569 total downloads.
https://www.nuget.org/packages/Microsoft.VisualStudio.Web.CodeGeneration.Design/
Example error:
Building project ...
Scaffolding failed.
Add Microsoft.VisualStudio.Web.CodeGeneration.Design package to the project as a
NuGet package reference. To see more information, enable tracing by setting
environment variable 'codegen_trace' = 1.

Set PackageId at package time?

I'm trying to figure out how to specify a packageId at build or specifically at package time. I have the following in my csproj which works well for creating packages locally for the purposes of testing. We are using Azure DevOps build pipelines to build and pack our nuget packages and I would like to be able to set the packageId as a task or msbuild parameter within the build pipeline.
Does anyone have suggestions on how I could acheive this?
Thanks,
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);Dependencies</TargetsForTfmSpecificBuildOutput>
<Version>0.1.0.5</Version>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyName>MyAssembly</AssemblyName>
<RootNamespace>MyAssembly</RootNamespace>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageId>MyAssembly.Test</PackageId> <----- set at package time
<FileVersion>1.0.0.0</FileVersion>
<PackageLicenseUrl>https://my.domain.xyz/license.pdf</PackageLicenseUrl>
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
<Description>A useful description.</Description>
<Company>XYZ</Company>
<Authors>UiPath</Authors>
<PackageIconUrl>http://my.domain.xyz/favicon.ico</PackageIconUrl>
Set PackageId at package time?
The answer is yes.
According to the document MSBuild pack target inputs, we could to know the PackageId is supported to be set at build time.
Note:
I saw that you set the property GeneratePackageOnBuild to true, so visual studio will generate the nuget package automatically.
So, if you are not use extra dotnet pack task to package your package, you add msbuild argument -p:PackageId=<PackageId> with your dotnet build task, like:
dotnet build -c Release -p:PackageId=<PackageId>
If you have another dotnet pack task to package your package, #Martin`s answer is correct.
Note2:
When we use the option -p:PackageId to change the package ID, but the AssemblyName is not changed. So the Package ID of the generated package is not consistent with its AssemblyName. We need to pay more attention when we use this nuget package. Or we could also change the AssemblyName to make it match the package id by the option -p:AssemblyName=<AssemblyName>
Hope this helps.
Did you try calling?:
dotnet pack -c Release -p:PackageId=The.Other.Packge.id
Or the equivalent in the .NET Core CLI task
You could modify the XML of the csproj after clone and before build using the File Transform task:
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/file-transform?view=azure-devops

How can I get GitVersion /UpdateAssemblyInfo to work with ASP.NET Core 2.0 project

We have been using a Bamboo build server for a while now and we have GitVersion installed so it can be selected as a task in the Build plan. We typically use the /UpdateAssembleInfo argument when we run the task. For .NET Framework projects, this would update the assemblyinfo file in the source with the bamboo versioning settings so the .NET assemblies had the same version info as our Bamboo builds and subsequent Bamboo deployment, allowing us to know the version of the deployed project in the field by examining the assembly file properties. This was all working quite well.
However, we are now building and deploying .NET Core 2.0 solutions and are finding that GitVersion /UpdateAssemblyInfo is not working.
I searched for a fix for .NET Core but was only able to find solutions that involved using the project.json file, which is no longer used with .NET Core 2.0 ( it changed to the *.csproj file).
I looked at http://gitversion.readthedocs.io/en/latest/usage/command-line/ and I tried running
gitversion.exe /UpdateAssemblyInfo MyProjectName.AssemblyInfo.cs /EnsureAssemblyInfo
where MyProjectName represents the actual project name suffix for the assemblyinfo.cs file in the .NET Core 2.0 ..\\obj\release\netcoreapp2.0 folder. But it did not update that file.
I have to assume that there has to be a solution for using GitVersion with Bamboo and.NET Core 2.0 but I am having a hard time finding one.
Any ideas?
The latest version of GitVersion provides /updateprojectfiles switch to update version info in the Sdk-style .csproj/.vbproj/.fsproj recursively.
From GitVersion/Usage/CommandLine/Arguments:
/updateprojectfiles
Will recursively search for all project files
(.csproj/.vbproj/.fsproj) files in the git repo and update them
Note: This is only compatible with the newer Sdk projects
It produces the needed attributes even if they are not present in the project files, resulting in following properties:
<Project>
<PropertyGroup>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<InformationalVersion>1.0.0-versionNumber.N+Branch.branchName.Sha.commitId</InformationalVersion>
<Version>1.0.0-versionNumberNNNN</Version>
</PropertyGroup>
As a workaround, you may consider specifying the assembly info as project properties in .csproj
<PropertyGroup>
<Version>1.2.3.4</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
...
</PropertyGroup>
and then setting values during dotnet build. In addition to its options, the dotnet build command accepts MSBuild options like /property
/property:name=value
/p:name=value
Set or override the specified project-level properties, where name is the property name and value is the property value. Specify each property separately, or use a semicolon or comma to separate multiple properties.
So your build command will be something like
dotnet build /p:Version=1.2.3.4;AssemblyVersion=1.2.3.4

An assembly specified in the application dependencies manifest (...) was not found

I upgraded Microsoft.AspNetCore from 2.0.3 to 2.0.5 and my WebAPI project, although running successfully locally, fails to start in production (IIS). Everything was fine in production until this upgrade. The error message produced in the log directory is as follows:
Error:
An assembly specified in the application dependencies manifest (MyProject.WebAPI.deps.json) was not found:
package: 'Microsoft.AspNetCore.Mvc.Abstractions', version: '2.0.2'
path: 'lib/netstandard2.0/Microsoft.AspNetCore.Mvc.Abstractions.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.5.xml
Could someone explain to me the details of exactly what this means? I assume it's a version mismatch of sorts, but why is this occurring? I thought the latest stable releases of NuGet packages weren't supposed to have such issues.
I was able to resolve the issue by downgrading Microsoft.AspNetCore.All from 2.0.5 to 2.0.3, but would like to find a better solution to the issue so I can use the most up-to-date version of this package.
Development machines usually have the SDK installed but on production the runtime only.
Add the following to your .csproj file and publish again.
<PropertyGroup>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
Sometimes this is related to the Startup Project, For example if the migration is a class library in Azure Functions project. You have to make sure when you run Add-Migration while the EF Library project is selected as Startup Project.
For me, the marked answer didn't solve the issue. My issue was when trying to Add-Migration
Add-Migration -Name initial-migration -Context Mysln.Data.MyDbContext -StartupProject Mysln -Project Mysln.Core
And the error was like this:
I solved it by downgrading all my Entityframework packages to 2.0.0 instead of the latest 2.2.0-preview one.
If you have more than one project in your solution like me:
and if you want to scaffold dbcontext in your "non startup" project (InstantOrder.Functions.Data in my case)
then you should add the -StartupProject parameter of the Scaffold-DbContext command like this -
Scaffold-DbContext "Server=..." -Project InstantOrder.Functions.Data -StartupProject InstantOrder.Functions.Data
I know this may be old, but just in case it can help some one else, this one worked for me:
Adding:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
To the PropertyGroup in the .csproj file.
To solve the first half of the error message, An assembly specified in the application dependencies manifest (…) was not found be sure to always use the publish output when deploying to a target sever.
For a self-contained application it can be found in
bin\Release\netcoreapp2.0\win81-x64\publish
or for framework-dependent deployments in
bin\Release\netcoreapp2.0\publish
The output in the directories above are meant to be used in development only, since they are specific to machine and user configuration built with.
Taken from a related answer.
2 cents: If you just take from the build folder, the dlls for the dependency aren't provided. If you publish the folder, they are. This was the fix for me.
I had this error however my solution was somewhat different from what was posted above. My problem was that I was deploying via a zip file and while building the zip file I wasn't including sub directories therefore required files were not being included.
So if you are publishing via a zip file make sure to include all sub folders while building the zip.
I got this error while running Scaffold-DbContext command on the Library project.
Solution:
Remove the Azure Function project from the solution, and then run this command.
After that, use add an existing project feature to add the Azure Function project again in the solution.
The correct .NET Core runtime was not installed on my PC. I had NETCore.App 2.1 and 2.2, but the project was targeted to 2.0.
dotnet --list-runtimes
I installed the correct runtime from the dot.net site and it resolved the issue.
In most case you get that error because there's misalignment of versions.
I changed the Microsoft.VisualStudio.Web.CodeGeneration.Design version, an it worked.
Before
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
After
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.4" />
This happened to me when I published my Lambda to AWS after renaming the project. I deleted both the obj and bin folders, rebuilt, republished and that fixed it.
I changed filters in Yaml.
Had projects called TestHelper etc...
Testrunner tried to run projects without tests, and the build was flagged as failed.
Added:
!**\*Helper*.*
To:
- task: DotNetCoreCLI#2
inputs:
command: 'test'
projects: |
**\*test*.dll
!**\*TestAdapter.dll
!**\obj\**
!**\*TestPlatform*.dll
!**\*Testing*.*
!**\*TestHost*.*
!**\*Helper*.*

Cannot create Nuget package that has xunit as dependency

Steps
Create a new .NETStandard 2.0 project e.g ClassLibrary1
Add xunit package as dependency
Open the command prompt and go to the location
Execute following two commands to create package
Please check dotnet pack for more details
dotnet build /p:SourceLinkCreate=true -v:n -c:Release -p:ci=true
dotnet pack -v:n -c=Release --no-build --include-source --include-symbols --output .\bin
Result: Package not created
Instead of xunit if you add any other dependency it works, for example NUnit and test framework, or Newtonsoft.Json. I have also tried with msbuild command, same result
Is there anything I am missing, or it's a bug?
This github issue says the behaviour is by design and your library needs the IsPackable setting in .csproj:
<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="15.0">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>true</IsPackable>
...