Skip to content

Instantly share code, notes, and snippets.

@sstorie
Created January 20, 2015 21:12
Show Gist options
  • Save sstorie/2b47c33fc82b0784331f to your computer and use it in GitHub Desktop.
Save sstorie/2b47c33fc82b0784331f to your computer and use it in GitHub Desktop.
Code to set up the ServiceLocatorProvider for use with MediatR
public static void Start()
{
var container = StructuremapMvc.StructureMapDependencyScope.Container;
// Create a new dependency resolver to use for this request, and save it so we can
// set up a service locator provider
//
var resolver = new StructureMapWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
// Now that we have a resolver to use for this request, we can update the
// container config to use that for locating services
//
// Note, we can't put this in the registry because it's not created until
// the request happens.
//
var provider = new ServiceLocatorProvider(() => resolver);
container.Configure(cfg =>
{
cfg.For<ServiceLocatorProvider>().Use(provider);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment