Remove those deprecated NuGet packages, use the ASP.NET Core shared framework instead
With the release of .NET Core 3.0, many ASP.NET Core assemblies are no longer published to NuGet as packages. Instead, the assemblies are included in the Microsoft.AspNetCore.App shared framework, which is installed with the .NET Core SDK and runtime installers. For a list of packages no longer being published, see Remove obsolete package references.
As of .NET Core 3.0, projects using the Microsoft.NET.Sdk.Web MSBuild SDK implicitly reference the shared framework. Projects using the Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference ASP.NET Core to use ASP.NET Core APIs in the shared framework.
To reference ASP.NET Core, add the following element to your project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
Reference
Referenced
I found the root cause is that the JSON generated by swagger is too large: Swagger outputs over 18000 lines for that API! Further research told me that two property fields contribute about 17000 lines. So how to configure swagger to ignore these two properties is the key. After googling a while, I found VeganHunter's answer in this thread Reference is the simplest.
Solution for .NET Core 3.1 and .NET Standard 2.1:
Use JsonIgnore from System.Text.Json.Serialization namespace.
( JsonIgnore from Newtonsoft.Json will NOT work )
public class Test
{
[System.Text.Json.Serialization.JsonIgnore]
public int HiddenProperty { get; set; }
public int VisibleProperty { get; set; }
}
Hope it could also help someone else. 😀
- Present enums as string. Modify your
StartUp.cs
, add the highlight part below:
services.AddMemoryCache().AddMvcCore().AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
})
.AddDataAnnotationsLocalization();
-
Enable XML Comments
- Manually add the two lines in PropertyGroup below to the .csproj file:
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
2. Add the options in the code below
services.AddSwaggerGen(options =>
{
options.IncludeXmlComments(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.xml"), true);
});
bash
ASPNETCORE_URLS="http://*:9143" ASPNETCORE_ENVIRONMENT="Development" dotnet Yourproject.dll
ASPNETCORE_ENVIRONMENT="Development" dotnet run --urls "http://*:9143" --no-launch-profile
for Windows command line (cmd.exe)
setx ASPNETCORE_URLS "http://localhost:5001"
setx ASPNETCORE_ENVIRONMENT "Development"
dotnet Yourproject.dll
for Windows powershell
$Env: ASPNETCORE_URLS "http://localhost:5001"
$Env: ASPNETCORE_ENVIRONMENT "Development"
dotnet Yourproject.dll
Reference