Skip to content

Instantly share code, notes, and snippets.

@btodts
Last active August 1, 2018 19:02
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 btodts/9fd101f92423b492d6e29860b58e0977 to your computer and use it in GitHub Desktop.
Save btodts/9fd101f92423b492d6e29860b58e0977 to your computer and use it in GitHub Desktop.
The autofac module that contains configuration
using System.IO;
using System.Reflection;
using Autofac;
using Newtonsoft.Json;
using Foo.Configuration;
using Module = Autofac.Module;
namespace Foo.Modules
{
public class ConfigurationModule : Module
{
protected override void Load(ContainerBuilder builder)
{
// Get and deserialize config.json file from Configuration folder.
var embeddedResourceStream = Assembly.GetAssembly(typeof(IConfiguration)).GetManifestResourceStream("Foo.Configuration.config.json");
if (embeddedResourceStream == null)
return;
using (var streamReader = new StreamReader(embeddedResourceStream))
{
var jsonString = streamReader.ReadToEnd();
var configuration = JsonConvert.DeserializeObject<Configuration>(jsonString);
if (configuration == null)
return;
builder.Register<IConfiguration>(c => configuration).SingleInstance();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment