19

I have created a nuget package where I am in need of adding a section to appsettings.json, or add my own configuration file that is copied into the application, but I am unable to figure out how.

I want this:

{
 "mysection" : 
  { 
        "value1": "value, 
        "value2": "value"
  }
}

to be added to the configuration file, or a file containing this to be copied when downloading the nuget package. We are using Visual Studio Team Services to build and host the nuget packages.

Richard Garside
  • 87,839
  • 11
  • 80
  • 93
ruffen
  • 1,695
  • 2
  • 25
  • 51
  • 2
    There is a request in Github for this feature, but the team say they don't plan to implement it: https://github.com/dotnet/extensions/issues/796 – Richard Garside Jan 15 '20 at 09:58
  • Here we are in 4 years and 5 months later, and I couldn't find a solution. Have you by any chance found a solution? – Ege Aydın Jan 30 '21 at 01:12
  • Unfortunately I don't have any other suggestions than the answers here, I haven't worked on the project where this was needed in about 3.5 years though... – ruffen Feb 10 '21 at 08:26

2 Answers2

4
  1. Go to properties of the file you want to be packed in the Nuget package(F4 or right click->properties) and change the 'build action' value to "EmbeddedResource"
  2. Right Click-> Edit Project File, then just add these lines:
    <ItemGroup>
        <EmbeddedResource Include="settings.json" Pack="true">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </EmbeddedResource>
    </ItemGroup>
Mansoor
  • 2,357
  • 1
  • 17
  • 27
Brachy
  • 74
  • 6
  • 1
    While this partially does the trick, copied files are not stored in the project but in the package itself and the project that uses the package gets a link to that file. If you have multiple projects using the same version of your package, they all would have to share the same file which is not desirable at all, in my opinion. After all this time, you would expect Microsoft to have a solution for this, but all my research has been to no avail. – Ege Aydın Jan 30 '21 at 02:05
1

You want to edit nuspec file and use files element and add files there.

<files>
    <file src="myConfig.json" target="Content" />
</files>

If you are using NuGet 3.3+ or PackageReference with NuGet 4+ you should use contentFiles elemenet instead.

<contentFiles>
    <files include="any/any/myConfig.json" buildAction="None" copyToOutput="true" flatten="true" />
</contentFiles>

Documentation recommends to specify both elements to achieve maximum compatibility. You can read more here

dropoutcoder
  • 2,627
  • 2
  • 14
  • 32
  • 4
    I think this would be more how to bring in an additional file, but what the author is looking for is how to get that settings section to merge into the applicationsettings.json file... similar to how you could add sections via transforms in the web.config. – Dylan Hayes Nov 05 '19 at 19:26
  • @DylanHayes: Ï am not sure, but I believe what are you talking about is solved already. There are enough of options how to configure things in ASP.NET Core => [Configuration in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.0). – dropoutcoder Nov 05 '19 at 20:31
  • @dropoutcoder I think Dylan means is being able to manipulate the appsettings.json file when the package gets installed. For example, adding a section that is specific to the package to the existing appsettings.json file, if it doesn't exist just create one. They used to have this in .Net Framework where you could manipulate the config files this way. But apparently, Microsoft does not plan to implement this future for .Net Core or 5. – Ege Aydın Jan 30 '21 at 02:11
  • @EgeAydın You don't need to manipulate appsettings.json. In ASP.NET Core you can load any json file, thus you can grab one from the package. – dropoutcoder Feb 13 '21 at 21:06