Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add configuration to appsettings.json with nuget package

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.

like image 511
ruffen Avatar asked Aug 15 '16 11:08

ruffen


2 Answers

  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>
like image 99
Brachy Avatar answered Sep 17 '22 16:09

Brachy


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

like image 31
dropoutcoder Avatar answered Sep 20 '22 16:09

dropoutcoder