8

So Im using autofac in a MVC so my controllers can have there dependencies injected on there constructor, I have in my Global.asax I have the following snippet of code, which works.

// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);

builder.RegisterType<PurchaseOrderSearchService>().As<IPurchaseOrderSearchService>().WithParameter("context", new PurchaseOrderManagerContext());

// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

The problem is I don't want to do builder.RegisterType over and over again, for all my Services. So how do I do that?

I think the kind of thing I want is

builder.RegisterAssemblyTypes(foo)
   .Where(t => t.Name.EndsWith("Services"))
   .WithParameter("context", new PurchaseOrderManagerContext());

But no idea what foo should be. Or if RegisterAssemblyTypes is correct way. I know coding by convention is the solution but not sure what the convention is. All my services will end int word Service and will have interface

so FooService will have interface IFooService and BarService will have interface IBarService

Should also point out that all the services live in a class library called PurchaseOrderManager.Service

1 Answer 1

10

You're on the right track. "Foo" should be the assembly containing the types to register - if you're using a single assembly then the following should work:

builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly)
    .Where(t => t.Name.EndsWith("Services"))
    .WithParameter("context", new PurchaseOrderManagerContext())
    .AsImplementedInterfaces();

The .AsImplementedInterfaces() is needed to register them as IFooService - without it, they would only be registered as FooService etc.

If your classes live in a separate assembly, I would normally recommend you define an autofac module within that assembly:

public class ServiceModule : Module 
{
    protected override void Load(ContainerBuilder builder)
    {
        // "ThisAssembly" means "any types in the same assembly as the module"
        builder.RegisterAssemblyTypes(ThisAssembly)
            .Where(....
    }
}

Then register this in your web application:

builder.RegisterModule<PurchaseOrderManager.Service.ServiceModule>();

Alternatively, use my original suggestion but explicitly specify the assembly containing the services:

builder.RegisterAssemblyTypes(typeof(PurchaseOrderManager.Service.FooService).Assembly)
    .Where(t => t.Name.EndsWith("Services"))
    .WithParameter("context", new PurchaseOrderManagerContext())
    .AsImplementedInterfaces();

You just need to pick any class which exists in that assembly.

3
  • Cheers, I think that is what I want... but I have all the services living in a class library called PurchaseOrderManager.Service so is throwing error None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'PurchaseOrderManager.Controllers.PurchaseOrderSearchController' can be invoked with the available services and parameters: Cannot resolve parameter 'PurchaseOrderManager.Service.Services.Interfaces.IPurchaseOrderSearchService service' of constructor 'Void .ctor(PurchaseOrderManager.Service.Services.Interfaces.IPurchaseOrderSearchService)'. Commented May 20, 2016 at 12:38
  • I went with the autofac model. I have protected override void Load(ContainerBuilder builder) { // "ThisAssembly" means "any types in the same assembly as the module" builder.RegisterAssemblyTypes(ThisAssembly) .Where(t => t.Name.EndsWith("Services")) .WithParameter("context", new PurchaseOrderManagerContext()) .AsImplementedInterfaces(); } But this will never get called, as its not overridding anything as its a class libary Commented May 20, 2016 at 13:57
  • Got this working, First problem were due to typing error. Commented May 24, 2016 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.