c# - ASP.NET CORE 1.0, AppSettings from another assembly -
i have application splittet 2 projects: web application , class library. startup
in web application:
var builder = new configurationbuilder() .addjsonfile("appsettings.json")
i wanna have appsettings.json in class library , being loaded there. possible? how can that?
the best solution have found requires creating new ifileprovider , ifileinfo, , embedding json settings files in assembly.
the solution reuses existing addjsonfile logic. way need tell configuration system how , locate json file, not how parse it.
the solution compatible .net core 1.0.
usage:
public class startup { private readonly appsettings _appsettings; public startup(ihostingenvironment env) { assembly assembly = typeof(yourclass).gettypeinfo().assembly; new configurationbuilder() .addembeddedjsonfile(assembly, "appsettings.json") .addembeddedjsonfile(assembly, $"appsettings.{env.environmentname.tolower()}.json") .build(); } ... }
embed files updating project.json class library:
... "buildoptions": { "embed": [ "appsettings.json", "appsettings.development.json", "appsettings.production.json" ] } ...
iembeddedfileinfo implementation:
public class embeddedfileinfo : ifileinfo { private readonly stream _filestream; public embeddedfileinfo(string name, stream filestream) { if (name == null) throw new argumentnullexception(nameof(name)); if (filestream == null) throw new argumentnullexception(nameof(filestream)); _filestream = filestream; exists = true; isdirectory = false; length = filestream.length; name = name; physicalpath = name; lastmodified = datetimeoffset.now; } public stream createreadstream() { return _filestream; } public bool exists { get; } public bool isdirectory { get; } public long length { get; } public string name { get; } public string physicalpath { get; } public datetimeoffset lastmodified { get; } }
ifileinfo implementation:
public class embeddedfileprovider : ifileprovider { private readonly assembly _assembly; public embeddedfileprovider(assembly assembly) { if (assembly == null) throw new argumentnullexception(nameof(assembly)); _assembly = assembly; } public ifileinfo getfileinfo(string subpath) { string fullfilename = $"{_assembly.getname().name}.{subpath}"; bool isfileembedded = _assembly.getmanifestresourcenames().contains(fullfilename); return isfileembedded ? new embeddedfileinfo(subpath, _assembly.getmanifestresourcestream(fullfilename)) : (ifileinfo) new notfoundfileinfo(subpath); } public idirectorycontents getdirectorycontents(string subpath) { throw new notimplementedexception(); } public ichangetoken watch(string filter) { throw new notimplementedexception(); } }
create easy use addembeddedjsonfile extension method.
public static class configurationbuilderextensions { public static iconfigurationbuilder addembeddedjsonfile(this iconfigurationbuilder configurationbuilder, assembly assembly, string name, bool optional = false) { // reload on change not supported, pass in false return configurationbuilder.addjsonfile(new embeddedfileprovider(assembly), name, optional, false); } }
Comments
Post a Comment