An open API service indexing awesome lists of open source software.

https://github.com/oscarbennich/nuget-tips-and-tricks

NuGet tips & tricks.
https://github.com/oscarbennich/nuget-tips-and-tricks

nuget tips-and-tricks

Last synced: 2 months ago
JSON representation

NuGet tips & tricks.

Awesome Lists containing this project

README

        

# NuGet Tips & Tricks

## Using a local NuGet feed

- A "local feed" is just a folder on your computer where we store packages that have been built locally and point to from other solutions (e.g. C:\Users\MYUSER\some\local\folder\nuget)

### Add required properties to `.csproj` file
```xml
[...]


[...]
1.0.0
NuGet package description...
README.md
$(USERPROFILE)\some\local\folder\nuget
$(USERPROFILE)\.nuget\packages


True
embedded


local

[...]
```

### Add build step to `.csproj` file to push package to local feed when building project with the "Debug" configuration
```xml



$(MSBuildProjectDirectory)\$(PackageOutputPath)$(ProjectName).$(PackageVersion)
$(LocalFeed)\$(PackageId)\$(PackageVersion)






```

### Add the local NuGet feed to the consuming solution

- In the `nuget.config` file in the repo, add this:

```xml




[...]

```

### Debugging packages from your local feed
- Thanks for the `embedded` statement that we added to the `.csproj` file above, the debug files will be automatically embedded with the source code when building the project with the `Debug` configuration.
- This means that debugging the package should work out-of-the-box and feel equivalent to debugging any other code that might be contained in the solution.
- This is because the embedded debug information will have a pointer to the file on your local computer and can therefore "jump" there, similar to opening the file yourself.
- You can even make changes directly in that file from the other solution.

## Building a new NuGet package using the version from the `` property (in an Azure DevOps pipeline)

1. Add a task to the pipeline call the `Determine-Version` script, this will update the build number based on the contents of the and what branch is being built:

```yml
############################
#### SET VERSION
############################

- task: PowerShell@2
displayName: "Determine and set version"
inputs:
pwsh: true
filePath: "scripts/Determine-Version.ps1"
arguments: >
-projectFile path/to/projectfile/project.csproj
-versionSuffix $(Build.BuildNumber)
-buildSourceBranch $(Build.SourceBranch)
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
```

2. Utilize the `byEnvVar` versioning scheme when running dotnet pack to use the build number we set in step 1:

```yml
############################
#### CREATE PACKAGE
############################

- task: DotNetCoreCLI@2
displayName: "📦 dotnet pack"
inputs:
command: "pack"
packagesToPack: "**/*.csproj"
configuration: "Release"
outputDir: "$(Build.ArtifactStagingDirectory)/"
versioningScheme: byEnvVar
versionEnvVar: BUILD_BUILDNUMBER
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
```

3. Push the created package to the Azure DevOps feed:

```yml

#################################
#### PUSH PACKAGE TO AZURE FEED
#################################

- task: DotNetCoreCLI@2
displayName: "🚀 dotnet push"
inputs:
command: "push"
packagesToPush: "$(Build.ArtifactStagingDirectory)/*.nupkg"
nuGetFeedType: "internal"
publishVstsFeed: "{{PRIVATE ORG FEED}}"
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
```

## Debugging packages pushed to an Azure DevOps feed
Debugging code that comes from a package in the Azure Artifacts feed is slightly more annoying, but works largely the same way as debugging the local NuGet packages described above.

For these packages we instead utilize the [Source Link library](https://github.com/dotnet/sourcelink) which is a Microsoft supported way to enable "first-class source debugging experiences for binaries".

### Add required properties to `.csproj` file

For this to work you need to add this to the `.csproj` file as well:

```xml


$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb

```

You also need to add a reference to the `Microsoft.SourceLink.AzureRepos.Git` NuGet package:

```xml

```

Now, when building the project with the `Release` configuration, the debug files (`.pdb`) will also be added together with the `.dll` files in the NuGet package.

### Disable the "Enable Just My Code" option
To be able to utilize debugging in this way in Visual Studio, you also need to disable the "Enable Just My Code" option:

- Open Visual Studio
- Go to "Debug"
- Go to "Options"
- Go to "General"
- Disable the "Enable Just My Code" option > Click OK

> NOTE: In normal cases it is recommended to have this option **enabled** for the optimal developer experience. So when you are done debugging the remote package, you should re-enable this option