Created
December 9, 2018 18:56
-
-
Save ulve/472012e016189825752a4f740378f5b5 to your computer and use it in GitHub Desktop.
Implementation for in memory json provider for use with ConfigurationBuilder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InMemoryJsonProvider : IFileProvider | |
{ | |
private IFileInfo _file; | |
public InMemoryJsonProvider(string json) | |
{ | |
_file = new MemoryFileInfo(json); | |
} | |
public IDirectoryContents GetDirectoryContents(string subpath) | |
{ | |
throw new NotImplementedException(); | |
} | |
public IFileInfo GetFileInfo(string subpath) | |
{ | |
return _file; | |
} | |
public IChangeToken Watch(string filter) | |
{ | |
throw new NotImplementedException(); | |
} | |
private class MemoryFileInfo : IFileInfo | |
{ | |
readonly byte[] _content; | |
public MemoryFileInfo(string json) | |
{ | |
Name = "config.json"; | |
_content = Encoding.UTF8.GetBytes(json); | |
LastModified = DateTime.Now; | |
} | |
public bool Exists => true; | |
long IFileInfo.Length => _content.LongLength; | |
public string PhysicalPath => null; | |
public string Name { get; } | |
public DateTimeOffset LastModified { get; } | |
public bool IsDirectory => false; | |
public Stream CreateReadStream() | |
{ | |
return new MemoryStream(_content); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment