Last active
August 1, 2018 19:02
-
-
Save btodts/9fd101f92423b492d6e29860b58e0977 to your computer and use it in GitHub Desktop.
The autofac module that contains configuration
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
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