ASP.NET Core Controllers Fail to Register with Authorization Package - asp.net-core

My asp.net core 3.1 project throws an exception if I include this package (context: I’m trying to create a custom Authorization policy):
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
This is the exception and method where it is thrown in startup.cs:
services.AddControllers();
System.ArgumentNullException: 'Value cannot be null. (Parameter 'configure')'
This is the complete list of packages. Is there some conflict that I'm not aware of? The package was suggested by VS when I created an AuthorizationHandler class.
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.0.1" />
<PackageReference Include="Azure.Identity" Version="1.2.3" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.1.0" />
<PackageReference Include="CorrelationId" Version="3.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.9" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.2.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.8.2" />

Oops, looks like the answer was already available with a little more effort on my part. I resolved this by manually downgrading the package version to 3.1.12 as described here. Looks like VS gave me the .net version of the package instead of the .net core version.

I was using an authorization handler form a .NET Standard 2 library where v.6.0.x was installed. Downgrading was not an option for me.
Exchanging services.AddControllers() with services.AddMvcCore() in my Core 3.1 project solved the problem for me.

Related

using FluentValidation.DependencyInjectionExtensions with asp.net-core6

I need to register FluentValidation in core6 Program.cs project file
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
And get an error
Cannot resolve scoped service 'Application.Validation.FluentValidation.ModelValidators.MyValidator' from root provider.
I have latest FluentValidation and FluentValidation.DependencyInjectionExtensions versions
<PackageReference Include="FluentValidation" Version="10.4.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="10.4.0" />

Refactor Custom ProblemDetailsFactory

As described and documented here, I like to customize the ProblemDetails hence extended ProblemDetailsFactory class.
However, I want to have this custom implementation to live in .Net Standard shared class library and not in the Web project itself. But I can't do that. It says that the base class ProblemDetailsFactory is not found.
I realized that the version of Microsoft.AspNetCore.Mvc.Core.dll referenced in OOB web application is 3.1 whereas its 2.2.5 in my class library project.
This is what my csproj file looks like:
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.6" />
<PackageReference Include="System.Reflection.Metadata" Version="1.8.1" />
</ItemGroup>
Is it something doable or thought through or am I missing something here?
Do not use nuget packages for aspnet stuff anymore
This works for a class library where you want aspnetcore libs available:
Documentation
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Upgrade .net core 2.2.0 to 2.2.6 or above

Looking for steps to upgrade .Net core 2.2.0 to 2.2.6 or higher (not 3.0)
I am new to .net core. So looking for guidance on how to upgrade .net core 2.2 to 2.2.6 as app is failing while pushing to Pivotal cloud foundry. Our PCF environment dotnetcore buildpack version is 2.2.12; Used to work before using 2.2.5 build pack. Please find error message below. To resolve this error , though of upgrading .net core app to versions higher than 2.2.5. But would like to seek help on steps to follow for upgrading the app & respective dependencies.
Here is targetframework from .csproj
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
And dependencies are
<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.1.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.1.0" />
<PackageReference Include="CsvHelper" Version="12.1.2" />
<PackageReference Include="EFCore.BulkExtensions" Version="2.4.6" />
<PackageReference Include="FluentValidation.AspNetCore" Version="8.3.0" />
<PackageReference Include="Manulife.Logging.DotNet" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Sendgrid" Version="9.11.0" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Steeltoe.CloudFoundry.Connector.EFCore" Version="2.2.0" />
<PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Version="2.2.0" />
<PackageReference Include="Steeltoe.Extensions.Logging.DynamicLogger" Version="2.2.0" />
<PackageReference Include="Steeltoe.Security.Authentication.CloudFoundryCore" Version="2.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
Error while pushing app to Pivotal (during dotnet publish)
depends on runtime.linux-x64.Microsoft.NETCore.App (>= 2.2.5) but run
time is not found. Picked up 2.2.6 which is available
/tmp/app/Basischangeapp/Basischange.csproj : error NU1605: Detected
package downgrade: Microsoft.NETCore.DotNetAppHost from 2.2.6 to 2.2.5.
Reference the package directly from the project to select a different
version.
It will happen because of version mismatch between local SDK and the build server.
Add this line to your .csproj file:
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
After that, RuntimeFrameworkVersion will use LatestNetCorePatchVersion.

How to resolve the Testserver problem in XUnit Test?

I am using xunit for testing my project. I was added all dependencies but i am getting following error.
The type or namespace name 'TestServer' could not be found (are you missing a using directive or an assembly reference?)
My test3.Tests.csproj dependencies
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
How can i resolve the problem.Any one try to help me thank you..
Try to install Microsoft.AspNetCore.Mvc.Testing.
Infrastructure components, such as the test web host and in-memory test server (TestServer), are provided or managed by the Microsoft.AspNetCore.Mvc.Testing package. Use of this package streamlines test creation and execution.
Test app prerequisites

Razor view compilation does not publish AppName.PrecompiledViews.dll

TLDR;
Razor view precompilation on the full framework (net471) only seems to work if the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation nuget package is added directly to the project and not within another package. Does anyone know a way around this?
I have a .net core 2.0 MVC project running on the full framework (net471). According to the docs, in order to perform view precompilation on a full framework app, you need to include the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation nuget package. This seems to work ONLY if you have added the package directly to the MVC project. If you have this package within another nuget package, the AppName.PrecompiledViews.dll never gets published.
I spun up a new .net core MVC project in visual studio running on net471 with the following packages. All works great, the dll shows up and performance indicates the views are compiled correctly:
Packages Used for Reference:
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.2" />
I next created a class library, added the same packages and bundled it in a nuget package (let's call it CommonLib). I created a second .net core 2.0 MVC project running on net471 but instead included the CommonLib nuget package. Everything runs normally, except on publish there is no AppName.PrecompiledViews.dll:
If I then add just the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation package to directly to the project with the CommonLib package, the PrecompiledViews.dll shows up:
I am trying to create a common class library project that contains dependencies for my projects (including .net core components) to simplify upgrades across all common micro projects. Has anyone had luck with doing this?
In order for Razor view compilation to run, you need to include special MSBuild targets that will automatically run as part of your project’s build process. These tasks are included in the Microsoft.AspNetCore.Razor.ViewCompilation package.
Currently, the only way to expose additional MSBuild targets with NuGet is by explicitly referencing the package. There is no transitive property with build targets, so you will always have to explicitly reference the package in order to activate those targets for your project.
I just found this issue on GitHub which suggests that build targets already flow transitively to the top project, but I actually haven’t found a way to trigger this behavior.