Skip to content

Instantly share code, notes, and snippets.

@ulve
Created December 9, 2018 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulve/472012e016189825752a4f740378f5b5 to your computer and use it in GitHub Desktop.
Save ulve/472012e016189825752a4f740378f5b5 to your computer and use it in GitHub Desktop.
Implementation for in memory json provider for use with ConfigurationBuilder.
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