Project specific Nuget.config with .net core/code

13,219

Solution 1

Assuming your project structure looks like:

Project/
└── src
    ├── SFMC.Adapter.Client
    ├── SFMC.Adapter.Service
    │   ├── Program.cs
    │   └── Project.csproj
    └── SFMC.Adapter.Service.Test

You can add a Nuget file anywhere within the project tree. Where you place it will affect which projects see it by default. dotnet restore will search for all Nuget.config files up the directory tree to add any sources it finds.

If you place it next to Service.cs, it will only be picked up by SFMC.Adapter.Service. If you place it under src it will be picked up and used by SFMC.Adapter.Service, SFMC.Adapater.Service.Test and SFMC.Adapter.Client.

See NuGet configuration docs for more details.

Solution 2

From the Nuget docs:

Project-specific NuGet.Config files located in any folder from the solution folder up to the drive root. These allow control over settings as they apply to a project or a group of projects.

So you can put a Nuget config file alongside the project file to give that project a specific configuration.

Solution 3

I know it is a rather old post, but I was struggling with it as well and had a hard time finding the correct documentation. I eventually found that you can execute the command below to add a new nuget config to the location where you are. dotnet new nugetconfig

This will use the dotnet cli to use the nugetconfig template (that appeared to be installed by default) to generate a correct file

Share:
13,219
Jon Raynor
Author by

Jon Raynor

Hey there! Been around for a while, since early 90s. My first job was troubleshooting EDI software and modems (9600 Baud!). Times have changed. Somewhere I have the original Netscape Navigator manual that explains how to use this crazy new tech called Javascript. Spent time on Microsoft tech with VB and C# in the 1990s and 2000s. Microsoft expert and consultant. Doing open source stuff on Pivotal cloud and AWS now. ELM is a nice web framework/platform to work with, check it out. Keep it simple stupid! (KISS) Have a nice day!

Updated on June 16, 2022

Comments

  • Jon Raynor
    Jon Raynor almost 2 years

    Normally, we get our Nuget.config from users\[loggedinuser\AppData\Roaming\Nuget but we have a case where a specific project where we need a use a different project specific config file. Where do I place the config file in this case?

    I am using .net core SDK 1.0.4 and .net code.

    The build script does this:

    cd src\SFMC.Adapter.Service
    dotnet restore
    dotnet build
    

    Can I pass an argument into dotnet restore to indicate the location of the config?

  • Martin Ullrich
    Martin Ullrich over 6 years
    minor input: NuGet will pick it up only for the file you run the restore on, so a project reference from test-> service shouldn't pick up the config located in the service project if you restore from within the test project.
  • Lion
    Lion over 4 years
    But the docs docs.microsoft.com/en-us/nuget/consume-packages/… say: In a solution folder, settings apply to all projects in subfolders. Note that if a config file is placed in a project folder, it has no effect on that project.
  • Tarek El-Mallah
    Tarek El-Mallah almost 3 years
    Confirmed that this is the best answer, Thanks , you saved my day.