using FluentValidation.DependencyInjectionExtensions with asp.net-core6 - fluentvalidation

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" />

Related

ASP.NET Core Controllers Fail to Register with Authorization Package

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.

OpenIdConnect .Net 5.0 blazor webassembly gives error on clean: no runtime pack for Microsoft.AspNetCore.App

If I use Nuget "Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0"
with .Net 5.0 blazor webassembly I get the following error on clean: NETSDK1082 There was no runtime pack for Microsoft.AspNetCore.App available for the specified RuntimeIdentifier 'browser-wasm'.
I created a brand new blazor webassembly .Net 5.0 project as in the below project file. The issue occurs when I include an openidconnect reference, but disappears if I delete that reference.
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>
</Project>
You can try to change your project file to below:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<UseBlazorWebAssembly>true</UseBlazorWebAssembly>
</PropertyGroup>
If it doesn't work,you can see more details in this thread.
Your question is not related to OpenIddict and is caused by the fact you're trying to reference a package - Microsoft.AspNetCore.Authentication.OpenIdConnect - that you're not supposed to use on Blazor.
It's a pure server-side package that CAN'T work on Blazor, which has its own OIDC authentication stack: https://www.nuget.org/packages/Microsoft.AspNetCore.Components.WebAssembly.Authentication/

Swashbuckle.AspNetCore: 'No operations defined in spec!' problem after update of 'Microsoft.AspNetCore.Mvc.ApiExplorer' package to 2.2.0

we have .net core 2.1 mvc webapi project which uses Swagger.
we use following packages:
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="4.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
swashbuckle is configured in following way:
services.AddMvcCore().AddVersionedApiExplorer();
services.AddApiVersioning();
services.AddSwaggerGen();
everything woks in such setup (/swagger/v1/swagger.json has all operations and definitions and UI is rendered properly -> having all controllers and actions, etc)
we are trying to migrate our .net core project from 2.1 to 2.2 .net core.
In order to do that (without warnings) we need upgrade Microsoft.AspNetCore.Mvc.ApiExplorer nuget from 2.1.2 to 2.2.0.
After this nuget update swagger.json (/swagger/v1/swagger.json) doesn't contain any "paths": {} and "definitions": {} and this results in swagger UI showing no controllers/actions (it renders: No operations defined in spec!
after upgrade package these package versions is updated:
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0" /> //was 2.1.2
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> //was 2.1.1
I've tried to upgrade swashbuckle version to v4.0.0 but it did not resolved the issue.
what im missing which packages needs to be updated also so swagger.json would be generated properly when using Microsoft.AspNetCore.Mvc.ApiExplorer 2.2.0 package ?
or i'm missing something else ?
Just had the same issue, thought I'd share my solution too. Version 6.0.4.
I had to add the [ApiController] attribute to my controllers. Swagger must be using that for discovery.
change public methods in controllers to [NoAction] Attribute.
Also, Change all actions with explicit action Methods to [HttpGet("api/get-customer")], [HttpPost("api/save-customer")] instead of [Route("api/get-customer")].
I found how to fix this issue (also posted to: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1166#issuecomment-497800414)
In short: i needed to change my HOST project SDK: from 'Microsoft.NET.Sdk' to 'Microsoft.NET.Sdk.Web'.
i did not find explicitly is it should be like that for sure (but default Microsoft template uses this SDK in .net core 2.2)
so it would be great if someone could explain why .Web SDK needs to be used after migration from .net core 2.1 to 2.2
Verify the decorator
[ApiController]
[Route("[controller]")]
Calling both app.UseSwagger(); and app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sonic App"); }); can cause this error as well.
Just add [ApiController] attribute to controllers. It works for Swashbuckle.AspNetCore 6.4.0

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.