-
-
Save justinyoo/ff0e90335d743362c7adf4cc7b3cef50 to your computer and use it in GitHub Desktop.
Managing Dependencies in Azure Functions
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 ServiceLocator | |
{ | |
public ServiceLocator() | |
{ | |
this.Build(); | |
} | |
public IServiceLocator Instance { get; private set; } | |
private void Build() | |
{ | |
var builder = new ContainerBuilder(); | |
// Register dependencies. | |
builder.RegisterType<MapperFactory>().As<IMapperFactory>().InstancePerDependency(); | |
builder.RegisterType<UserService>().As<IUserService>().InstancePerDependency(); | |
var container = builder.Build(); | |
// Create service locator. | |
var csl = new AutofacServiceLocator(container); | |
// Set the service locator created. | |
Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(() => csl); | |
// Use the service locator. | |
this.Instance = Microsoft.Practices.ServiceLocation.ServiceLocator.Current; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment