ASP.NET Hosting

ASP.NET Tutorial: Package Validation In .NET

In this article, we are going to learn about package validation in .Net

package validation was introduced in .NET 6 SDK. It allows developers to ensure the packages we get from NuGet package managers and even verify whether the NuGet package manager is consistent.

Package validation helps us in following things to verify

  1. It allows the developer to validate that there are no breaking changes across all the versions of packages.
  2. Package Validation allows developers to validate that all our packages are consistent and well-formed.
  3. It will help developers catch any applicability holes.
  4. Validates that all packages have the same set of public APIs for different runtime-specific implementations.

Setup package validation in Project

To set up the package validation for the application, we need to set up a property in csproj file like below.

To set up this, we need to make the EnablePackageValidation property true

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnablePackageValidation>true</EnablePackageValidation>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
  </ItemGroup>
</Project>

How to validate the packages?
1. Validating Compatible Frameworks

The Validating Compatible Frameworks validate that the code compiled against one framework can run against all the other frameworks in the multi-targeting packages.

Package Validation will catch all the errors at the package installation time, for ex.

Suppose we are writing a game code in which we do many string manipulation. We need to support both the .NET Framework and the .NET Core consumers. So we started with targeting .NET Standard 2.0, but later on, we realize we want to use spans in .NET 6.0 to avoid the unnecessary string allocations, So to do that, we now want the multi-target for .NET Standard 2.0 and .NET 6.0, below is the code for same.

#if Framework NET6_0_OR_GREATER
public void DoStringManipulation(ReadOnlySpan < char > val) {
    // use te spans to do the string operations.
}
#else
public void DoStringManipulation(string val) {
    // Do the string operations.
}
#endif

Now if we install the package in the project (using DotNet package manager or cmd) it will give the below error

So as shown in error, we should not exclude DoStringManipulation(string). Instead, we need to give another DoStringManipulation (ReadoOnlySpan<char>) method for .NET 6.0, so the code will be like below

#if NET6_0_OR_GREATER
public void DoStringManipulation(ReadOnlySpan < char > val) {
    // use te spans to do the string operations.
}
#endif
public void DoStringManipulation(string val) {
    // Do the string operations.
}

2. Baseline version validator

The Baseline version validator validates the packages library project against the previously released packages, basically the stable version. Package validation identifies any breaking change.

To use it, we need to add the PackageValidationBaselineVersion or PackageValidationBaselinePath in our project.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnablePackageValidation>true</EnablePackageValidation>
    <PackageVersion>2.0.0</PackageVersion>
    <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
  </ItemGroup>
</Project>

We can even use the PackageValidationBaselinePath property as well to identify the breaking changes

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnablePackageValidation>true</EnablePackageValidation>
    <PackageVersion>2.0.0</PackageVersion>
    <PackageValidationBaselinePath>PackagePath</PackageValidationBaselinePath> >
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
  </ItemGroup>
</Project>

3. Validation Against Diff Runtimes

The Compatible runtime validator validates run times assemblies that are compatible with the compile time assemblies.

ASP.NET Core 7.0.2 Hosting Recommendation

HostForLIFE.eu

HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.

A Good Web Host Saves Time and Helps You Improve the Blog

I’m sure that you have had your reasons to get started with a free web host. Unfortunately, you will end up losing precious time tackling the issues that come with it.

So, you should save up a little and get any of the best shared hosting providers to get complete control over your website.

And, if you want the best for your WordPress blog and decide to spend more, I’d place my bets on managed hosting options.