Posted on April 21, 2023
This tool, through which a developer can create, share and consume code from geographically distributed teams, also helps the teams follow standard guidelines and code practices. A shared package can be used in multiple projects with a one-time investment, and easy-to-update software centrally without impacting many consumers. Simply put, a NuGet package is a ZIP file generated with a Let's understand the flow of the NuGet package from creator to consumer Let's take an example that we have multiple projects in the firm and everyone wanted to consume or enable "Application Insights telemetry collection" to the application to achieve there could possibly be two approaches. First Approach, everyone writes their own code in different projects to enable "Application Insights telemetry collection" Second Approach, creating a shared library and a adding middleware extension over in the library and then publishing assemblies to public/private host (NuGet/Azure DevOps). The source code for this article can be found on GitHub. Create a library package that adds or registers AddApplicationInsightsTelemetry() to an application Create a new class library project in Visual Studio. You can name it whatever you like, for example, "ApplicationInsightsLibrary". Add the following NuGet packages to the project: In the Inside the Inside the AddApplicationInsightsTelemetry method, add the following code to configure Application Insights telemetry: A developer can generate a manifest with help of different tools To build and generate a .nupkg file, run the dotnet pack command. We have an automatic way of generating packages as well whenever build the project. You need to add the following lines in the project file Make sure to copy the path from the output window which looks like this: To consume a NuGet package locally, you can follow these steps. Now you have completed configuring the package source, you can go to the consumer application now and right-click on the project or solution and then "Manage NuGet Package > Browse" as in the snapshot below.
Make sure to change the package source from the right-hand corner icon. Here is an example of a YAML pipeline that builds a .NET Core application, runs unit tests, and publishes the resulting artifacts to Azure Artifacts Create a feed: Create a feed in your Azure DevOps organization to store your packages. To create a feed, go to the Artifacts section of your project and click on the "Create Feed" button. Configure your pipeline: In your pipeline, add a task to publish the package to the feed. The exact configuration of this task depends on the format of your package. Here are some examples: Sign in to your NuGet.org account or create one if you haven't. Select your user name icon then select API Keys. Select Create then enter a name for your key. Give your key a Push new packages and package version scope, and enter * in the glob pattern field to select all packages. Select Create when you are done. Here is a more official article about it. .NET package management using NuGet is a popular method of managing dependencies in .NET projects. NuGet is a package manager for .NET that enables developers to easily find, install, and update third-party libraries and tools in their projects. NuGet packages are versioned to enable developers to track changes and ensure consistency in their projects. Version numbers are composed of three parts: major version, minor version, and patch version. Developers can use semantic versioning to determine how to update package versions based on the changes made to the package. In addition to managing dependencies, NuGet can also be used to create and publish packages to both public and private NuGet feeds. When creating a package, developers can specify the version number, dependencies, and other metadata. NuGet also supports the versioning of packages, enabling developers to track changes and manage updates. In order to manage NuGet packages and versions in .NET projects, developers can use the NuGet Package Manager in Visual Studio, the dotnet CLI, or YAML pipelines in Azure DevOps. Developers can restore, install, update, and remove packages as needed, and specify the version of a package to ensure consistency across different environments. Overall, NuGet and versioning provide powerful tools for .NET developers to manage dependencies and ensure consistency in their projects, simplifying the development process and improving code quality.What is .NET package management?
.nupkg
extension containing DLLs and other manifest files that include information about the package. The developer generally shares published code with the public and private host, then a consumer retrieves the package from the host to the local machine.What we are building here?
Creating a package or library
Install-Package Microsoft.ApplicationInsights.AspNetCore
Install-Package Microsoft.Extensions.DependencyInjection
codehacks-shared-package-lib
project, create a new class named ApplicationInsightsExtensions
.ApplicationInsightsExtensions
class, create a public static method named AddApplicationInsightsTelemetry
that takes an IServiceCollection
parameter.using Microsoft.Extensions.DependencyInjection;
namespace codehacks_shared_package_lib
{
public class ApplicationInsightsExtensions
{
public static void AddApplicationInsightsTelemetry(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry();
}
}
}
dotnet CLI
nuget.exe CLI
MSBuild
Pack Command
dotnet pack
propertyGroup
tag.<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
C:\Code\Playground\codehacks-shared-package-lib\bin\Release\codehacks-shared-package-lib.1.0.0.nupkg
Consuming in NuGet package Locally
dotnet pack
command per the instructions above or locate a .nupkg file from your package.Tools > NuGet Package Manager > Package Manager
. see the snapshot below.
Package Source -> + icon -> enter copied folder path -> Hit OK
(like 'C:\Code\Playground\codehacks-shared-package-lib\bin\Release')
AddApplicationInsightsTelemetry
method in the ConfigureServices
method of your Startup classpublic void ConfigureServices(IServiceCollection services)
{
// Add other services here
// Add Application Insights telemetry
ApplicationInsightsExtensions.AddApplicationInsightsTelemetry(services);
}
Publishing package to Artifact feed in Azure DevOps
trigger:
- main
variables:
buildConfiguration: 'Release'
versionSuffix: '$(Build.BuildNumber)'
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
inputs:
version: '6.x'
includePreviewVersions: true
- task: DotNetCoreCLI@2
displayName: 'Restore NuGet packages'
inputs:
command: 'restore'
projects: '**/*.csproj'
feedsToUse: 'config'
nugetConfigPath: './NuGet.config'
- task: DotNetCoreCLI@2
displayName: 'Build project'
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration) /p:VersionSuffix=$(versionSuffix)'
- task: DotNetCoreCLI@2
displayName: 'Create NuGet package'
inputs:
command: 'pack'
packagesToPack: '**/*.csproj'
versionSuffix: '$(versionSuffix)'
configuration: '$(buildConfiguration)'
includeSymbols: true
outputDir: '$(Build.ArtifactStagingDirectory)/NuGetPackages'
- task: DotNetCoreCLI@2
displayName: 'Push NuGet package to feed'
inputs:
command: 'push'
nuGetFeedType: 'internal'
packagesToPush: '$(Build.ArtifactStagingDirectory)/NuGetPackages/*.nupkg'
nuGetFeedPublish: 'my-nuget-feed'
versioningScheme: 'byEnvVar'
versionEnvVar: 'BUILD_BUILDNUMBER'
Publish packages to NuGet.org
Summary