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

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

Related

Downgrade from Core 3 to 2 then Microsoft.AspNetCore.App not recognised

I have VS2019 v16.3.5 installed along with the dotnet core 3 sdk. I can browse to C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App and see lots of the versions of the shared framework in folders from 2.1.9 to 3.0.0
To reproduce the problem:
create new console app
edit project file to add:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
At this point it works ok but then:
Change target framework to 2.2
I get the build error:
The FrameworkReference 'Microsoft.AspNetCore.App' was not recognized
Should it be possible to target 2.2 and still use the shared framework Microsoft.AspNetCore.App?
FrameworkReference is a new feature of ASP.NET Core 3.0. Not available in the lower versions. For lower versions replace <FrameworkReference Include="Microsoft.AspNetCore.App" /> with the following:
<PackageReference Include="Microsoft.AspNetCore.App" />

Cannot find `SignalR` in `Microsoft.AspNetCore.App` in .NET Core 2.2?

I'm not so sure why this can be a problem, I've just revised some documentation about SignalR in ASP.NET Core and it seems to mean that I don't have to install any additional package (already have Microsoft.AspNetCore.All and Microsoft.AspNetCore.App referenced after creating a new ASP.NET Core project (version 2.2).
I've even tried Googling and found this blog post: https://wakeupandcode.com/real-time-asp-net-core-web-apps-with-signalr/#dep
It was written on Dec 23 2018, and the author does confirm that we don't need to install any additional package.
Well so it's really playing on me, making me have a feeling of being stupid, not so sure why I cannot use SignalR in my project (I'm not intending to install any additional package, which I think should work for me), just wonder why it's not already available. The following code does not compile:
using Microsoft.AspNetCore.SignalR;
It reports that SignalR does not exist in the namespace Microsoft.AspNetCore right in the project that has Microsoft.AspNetCore.All and Microsoft.AspNetCore.App.
Actually I cannot find the Microsoft.AspNetCore.SignalR when expanding the references tree.
The documentation says nothing about where to import the module (which means it should already be available). As I said the project targets .NET Core 2.2. Could you explain to me something could be wrong here?
PS: My project is a Web API (not MVC) project.
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.4" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
</ItemGroup>
Usually, project templates have outdated libraries. Use the NuGet package manager to update references to recent versions and try again. It should help.

how to setup EFcore database first in separate class library in ASP.NET Core MVC application?

I have a DB and I want to add a separate class library for entity framework to access my data but when I run scaffold command it makes a DB model in my web application.
You should select your Data project as default project in package manager console.
Be careful, you have ef core references in data.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0-preview3-35497" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.0-preview3-35497"/>
<PackageReference Include="System.Data.Common" Version="4.2.1" />
</ItemGroup>
</Project>
I realized that there are some errors in ClassLibrary projects. You can take a look at them:
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#targeting-class-library-projects-is-not-supported
https://github.com/dotnet/cli/issues/8735
https://github.com/aspnet/EntityFrameworkCore/issues/10298
#ibrahimozgon's answer is right and helped me. However, I encountered a few errors in the DbContext class on the way which he didn't mention how to solve:
'EntityTypeBuilder' does not contain a definition for 'ToTable' and no accessible extension method 'ToTable' accepting a first argument of type 'EntityTypeBuilder' could be found (are you missing a using directive or an assembly reference?
and
'KeyBuilder' does not contain a definition for 'ForSqlServerIsClustered' and no accessible extension method 'ForSqlServerIsClustered' accepting a first argument of type 'KeyBuilder' could be found (are you missing a using directive or an assembly reference?
To resolve these errors in DbContext class, open up package manager console again and select the default project to be the class library. Enter these commands one by one:
- Install package: Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.4 (or whichever latest version)
- Install package: Install-Package Microsoft.EntityFrameworkCore.Relational -Version 2.2.4 (or whichever latest version)
- If errors persist try: Install-Package or Update-Package Microsoft.EntityFrameworkCore.Tools (or whichever latest version)

Netcore 2.1.1 release leads to app failing to run

It seems there is suddenly something out of sync with asp.net core packages.
It looks like Microsoft.AspNetCore.App 2.1.1 was released 16 hours ago on nuget.org but only 158 downloads, and apparently I'm one of the downloaders.
My .csproj file is like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\cloudscribe.Web.Navigation\cloudscribe.Web.Navigation.csproj" />
<ProjectReference Include="..\cloudscribe.Web.SiteMap\cloudscribe.Web.SiteMap.csproj" />
<ProjectReference Include="..\cloudscribe.Web.SiteMap.FromNavigation\cloudscribe.Web.SiteMap.FromNavigation.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.*" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.*" />
</ItemGroup>
</Project>
It builds but when I try to run it I get:
It was not possible to find any compatible framework version
The specified framework 'Microsoft.AspNetCore.App', version '2.1.1' was not found.
- Check application dependencies and target a framework version installed at:
C:\Program Files\dotnet\
- Installing .NET Core prerequisites might help resolve this problem:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
- The .NET Core framework and SDK can be installed from:
https://aka.ms/dotnet-download
- The following versions are installed:
2.1.0 at [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
If I change my version for Microsoft.AspNetCore.App to 2.1.0 and do dotnet restore I get this:
C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj : error NU1107: Version conflict detected for Microsoft.AspNetCore.Antiforgery. Reference the package directly from the project to resolve this issue.
C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj : error NU1107: NavigationDemo.Web -> cloudscribe.Web.Navigation -> Microsoft.AspNetCore.Mvc.Razor 2.1.1 -> Microsoft.AspNetCore.Mvc.ViewFeatures 2.1.1 -> Microsoft.AspNetCore.Antiforgery (>= 2.1.1)
C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj : error NU1107: NavigationDemo.Web -> Microsoft.AspNetCore.App 2.1.0 -> Microsoft.AspNetCore.Antiforgery (= 2.1.0).
Restore failed in 3.54 sec for C:_c\cloudscribe.Web.Navigation\src\NavigationDemo.Web\NavigationDemo.Web.csproj.
I suppose that is partly due to the other projects in my solution with dependencies like this:
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.*" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.*" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.*" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.1.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.*" />
There does not seem to be an updated sdk for 2.1.1 but the availability of Microsoft.AspNetCore.App 2.1.1 on GitHub seems to be breaking things for me.
The 2.1.1 (preview) SDK is available on their GitHub.
You can download directly here:
https://dotnetfeed.blob.core.windows.net/orchestrated-release-2-1/20180605-09/final/assets/Sdk/2.1.301-preview-008906/dotnet-sdk-2.1.301-win-x64.exe
See:
https://github.com/dotnet/versions/tree/7a833dddfddc27f2074b755b94234a25b9757637/build-info/dotnet/product/cli/release/2.1
We are still waiting on the official SDK...
Edit:
If you are having trouble building, add the following to your .csproj
<PropertyGroup>
<TargetLatestAspNetCoreRuntimePatch>true</TargetLatestAspNetCoreRuntimePatch>
</PropertyGroup>
Update:
Official 2.1.1 SDK now released: https://www.microsoft.com/net/download/thank-you/dotnet-sdk-2.1.301-windows-x64-installer
Your problem is this line of your csproj:
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.*" />
The * is saying to pick the latest 2.1 version of that NuGet package. For many packages, this would be perfectly fine. However, v2.1.1 of that package requires a matching v2.1.1 SDK to also be installed. As of right now, it is not available (it's currently blocked).
However, if you read the Migrate from ASP.NET Core 2.0 to 2.1 docs, you will see this:
Replace the version specified "Microsoft.AspNetCore.All" package reference with the versionless "Microsoft.AspNetCore.App" package reference.
The version is now inferred by the version of the SDK you are targeting. This means your csproj should now contain this:
<PackageReference Include="Microsoft.AspNetCore.App" />
In addition to other suggestions, I needed to up the Microsoft.NetCore.App to 2.1.1. Since I couldn't do it via the UI, adding the following to the .csproj worked.
<PackageReference Include="Microsoft.NetCore.App" Version="2.1.1" />
I had the same issue. Another option is, in NuGet Package Manager, rollback Microsoft.AspNetCore.All, Microsoft.AspNetCore.Rewrite, and Microsoft.NETCore.App to versions 2.1.0.
The wildcard version number package references is weird to me. That doesn't happen by default, so either you or one of your team members did that. I don't know if that's the source of at least some of your issues or not, but I'd start by changing them to full version references. NuGet packages are not committed with your project, so having wildcards can lead to strange build issues, particularly in automated builds, when you're developing against one version but a different version is restored later. If you want to upgrade a package, that should be an explicit choice, so you can then verify that all your code still functions as it should post-upgrade.
Also worth noting, since 2.1, code generation has been rolled in, so you should not be explicitly referencing any CodeGen packages.
As far as solving the current issue goes. You may be having some issues with your package cache. In Visual Studio, go to Tools > NuGet Package Manager > Package Management Settings, and then click the "Clear All NuGet Cache(s)" button.
That should prompt a redownload of all your projects NuGet packages, and may be enough to solve the issue. If not, I'd recommend cleaning all your projects and then going into the directory for each and manually deleting both the bin and obj directories. Then, rebuild your solution.
For bugged SDK 2.1.301 version use:
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
and force all other packages 2.1.1 -> 2.1.0.
Installing 2.1.301 (https://github.com/dotnet/core/blob/master/release-notes/download-archives/2.1.1-download.md) fixed this issue for me.

Could not find 'UserSecretsIdAttribute' on assembly 'ef'

I am using ASP.NET Core 1.1 and I have the following on startup:
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddUserSecrets();
I am using csproj file instead of JSON where I added:
<PropertyGroup>
<UserSecretsId>8844d677-223b-4527-a648-387a65933d55</UserSecretsId>
</PropertyGroup>
But when I run the command:
dotnet ef migrations add "InitialCommit"
I get the error:
An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Could not find 'UserSecretsIdAttribute' on assembly 'ef, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"
Any idea why?
You have run into this issue: https://github.com/aspnet/Configuration/issues/543
The solution is to change your call .AddUserSecrets() to .AddUserSecrets(Assembly assembly)
See this announcement about how deprecating project.json required a breaking change to user secrets: https://github.com/aspnet/Announcements/issues/209
I had this issue also. My solution was to install the nuget package Microsoft.Extensions.Configuration.UserSecrets.
It finally worked for me when I changed a version of package in .csproj:
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
to:
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />