Registering a service using Autofac and getting the list of services via Container in MVC Controller

Using a DI framework (I am using Autofac in my case), you can register the services like below  in AutofacConfig.cs class
inside Configure() method as below

var builder = new ContainerBuilder();

builder.RegisterType<MyClass>().As<IMyInterface>();

Now you can loop through the list of services in the container .You can either get the entire list of services , or

get the specific service based on interface as below

           var container = GlobalConfiguration.Configuration.DependencyResolver.GetServices(typeof(IMyInterface));
            
             foreach (IMyInterface dependentServc in container)
             {
                 var serviceResponse = dependentServc.ServiceMethod();
                 ….

                ….//Your business logic here
             }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s