Package a Razor class library

 A task that you often need to perform is packaging libraries for reuse by other developers. NuGet packaging makes it trivial for any developer anywhere to acquire and properly configure all the .NET references for their applications.

In the preceding unit, you built your modal dialog component and used it in your own application. Now you want to reuse it in other applications.

In this unit, you learn the steps necessary to configure a Razor class library as a NuGet package. You also learn how to package the library for distribution by using a package-repository service, such as NuGet.org, or GitHub repositories.

Configure a Razor class library for NuGet packaging

The .NET ecosystem makes it easy to define the properties that are necessary for other developers to identify and use your components. You can define all these properties in the project file (*.csproj) of your Razor class library so they travel with the library. The properties are then updated appropriately when your library is updated.

You can configure fields that identify your package in the Visual Studio Project Properties - Package dialog, or you can create entries directly in the *.csproj file yourself.

The four fields required to create a package are:

FieldDescriptionDefault value
PackageIdA package identifier, unique across the entire NuGet repository.AssemblyName of the library
VersionA specific version number in the form Major.Minor.Patch[-Suffix], where -Suffix optionally defines prerelease versions.1.0.0
AuthorsThe authors of the package.AssemblyName
CompanyThe name of the company that's responsible for creating and publishing the package.AssemblyName

Some of these fields have default values, and it might look funny to publish a package with a company name of MyFirstLibrary. We strongly recommend that you explicitly define these values.

In the preceding unit, you learned that the static content of a Razor class library is available at _content/[PACKAGE_ID]/, and now you see where the PackageId value is configured.

A sample project file with these values configured might look like the following example:

XML
<PropertyGroup>
    <PackageId>Learn.MyFirstLibrary</PackageId>
    <Version>0.1.0-alpha1</Version>
    <Authors>Susan Developer, Terry Programmer</Authors>
    <Company>AdventureWorks</Company>
</PropertyGroup>

When you're building the project, you can also configure it to generate a NuGet package either by selecting the option to Generate NuGet Package on Build in the Visual Studio Project Properties dialog or by adding a GeneratePackageOnBuild entry next to the other package fields, as shown here:

XML
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

You can configure many optional project properties, including:

  • Description that's appropriate for display in the NuGet repository
  • A copyright notice
  • Licensing information
  • Icons
  • Project URLs

For a full list of properties, check the Microsoft documentation on the MSBuild pack target.

Package the library

After you write the library configuration into the *.csproj file, you can generate the NuGet package either in Visual Studio, by right-clicking the project and selecting the Pack command, or in the project folder, by running the following command:

.NET CLI
dotnet pack

This dotnet pack command generates a package with the PackageID and version number and places it in the standard project build output folder.


Please attend iot internet of things training courses malaysia https://lernix.com.my/iot-internet-of-things-training-courses-malaysia/

Comments

Popular posts from this blog

Electrical Wiring in malaysia

Rooftop Solutions for Sustainable Living in Malaysia

piping works in malaysia