Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created November 21, 2016 02:03
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 justinyoo/ff0e90335d743362c7adf4cc7b3cef50 to your computer and use it in GitHub Desktop.
Save justinyoo/ff0e90335d743362c7adf4cc7b3cef50 to your computer and use it in GitHub Desktop.
Managing Dependencies in Azure Functions
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