Dani February 2016

ASP.NET CORE 1.0, AppSettings from another assembly

I have an application splittet into two projects: a web application and a class library. The Startup is only in the web application:

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")

I wanna have my appsettings.json in that class library and being loaded from there. Is that possible? How can I do that?

Answers


Joe Audette February 2016

Yes you could implement IConfigurationProvider

There is a base class ConfigurationProvider that you can inherit from then override all the virtual methods.

You can also see how the JsonConfigurationProvider is implemented.

So I guess your implementation could use the Json provider code internally against embedded json files.

Then you would also want to implement ConfigurationBuilder extension to register your provider similar as the code for using json config.


Baaleos February 2016

Someone else can correct me, but I don't think what you are looking for exists. App Configs and AppSettings files are read at runtime by the application that is running.

The Class Library cannot see any AppSettings specific to itself, because when it runs at run time, it is in the folder of the running application.

The only potential way I can see for you to get your class library contain the json file, is to have the json file as an embedded resource. Eg: In the solution, select the json file, and set it to Embedded Resource instead of 'content'.

The problem becomes getting the embedded config file out of your assembly, and then loaded.

AddJsonFile accepts a path to the json file.

You could however extract the Json file to a temp directory, then load from there.

static byte[] StreamToBytes(Stream input)
            {

                int capacity = input.CanSeek ? (int)input.Length : 0; 
                using (MemoryStream output = new MemoryStream(capacity))
                {
                    int readLength;
                    byte[] buffer = new byte[capacity/*4096*/];  //An array of bytes
                    do
                    {
                        readLength = input.Read(buffer, 0, buffer.Length);   //Read the memory data, into the buffer
                        output.Write(buffer, 0, readLength);
                    }
                    while (readLength != 0); //Do all this while the readLength is not 0
                    return output.ToArray();  //When finished, return the finished MemoryStream object as an array.
                }

            }



Assembly yourAssembly = Assembly.GetAssembly(typeof(MyTypeWithinAssembly));
using (Stream input = yourAssembly.GetManifestResourceStream("NameSpace.Resources.Config.json")) // Acquire the dll from local memory/resources.
                {
                    byte[] byteData  = StreamToBytes(input);
                    System.IO.File.Write 


Dani February 2016

Here is my solution, thanks Baaleos and Joe for your advices.

project.json

"resource": [
    "appsettings.json"
  ]

startup.cs

var builder = new ConfigurationBuilder()
    .Add(new SettingsConfigurationProvider("appsettings.json"))
    .AddEnvironmentVariables();

this.Configuration = builder.Build();

namespace ClassLibrary
{
    using Microsoft.Extensions.Configuration;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;

    /// <summary>
    /// A JSON file based <see cref="ConfigurationProvider"/> for embedded resources.
    /// </summary>
    public class SettingsConfigurationProvider : ConfigurationProvider
    {
        /// <summary>
        /// Initializes a new instance of <see cref="SettingsConfigurationProvider"/>.
        /// </summary>
        /// <param name="name">Name of the JSON configuration file.</param>
        /// <param name="optional">Determines if the configuration is optional.</param>
        public SettingsConfigurationProvider(string name)
            : this(name, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of <see cref="SettingsConfigurationProvider"/>.
        /// </summary>
        /// <param name="name">Name of the JSON configuration file.</param>
        /// <param name="optional">Determines if the configuration is optional.</param>
        public SettingsConfigurationProvider(string name, bool optional)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name must be a non-empty string.", nameof(name));
            }

            this.Optional = optional;
            this.Name = name;
        }

        /// <summary>
        /// Gets a va 

Post Status

Asked in February 2016
Viewed 3,171 times
Voted 11
Answered 3 times

Search




Leave an answer