c# - How to get container for Autofac for WebAPI2? -
in ninject can object needed interface using class webcontainermanager
ninject definition:
var logmanager = new logmanageradapter(); container.bind<ilogmanager>().toconstant(logmanager);
ninject usage:
var log = webcontainermanager.get<ilogmanager>().getlog(typeof(webapiapplication));
my question how same in autofac, needed class interface?
update 1: im using webapi 2, not mvc.
you can create builder.
var builder = new containerbuilder(); // you're interested in exposing type // via interface: builder.registertype<sometype>().as<iservice>(); // however, if want both services (not common) // can so: builder.registertype<sometype>().asself().as<iservice>();
then able build ioc:
icontainer container = builder.build();
and simple example of how resource container:
// create scope, resolve iservice, // use it, dispose of scope. using (var scope = container.beginlifetimescope()) { var writer = scope.resolve<iservice>(); writer.dosomething(); }
Comments
Post a Comment