How to use autofac in .NET core to use conventions to find and register components like this
builder.RegisterAssemblyTypes(dataAccess)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
I found builder.RegisterControllers(Assembly.GetExecutingAssembly()).Where(t=>t.Name.StartsWith("Home"));
that can't use. how can Injected xxxService into controller
Take a look at this Microsoft Doc
All you have to do is Register the service with the autofac container. For example, assuming your service types end with 'Service' you could do:
builder.RegisterAssemblyTypes(assemblies)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
Then use constructor injection to inject the service into the controller:
public class HomeController
{
private IFooService fooService;
public HomeController(IFooService fooService)
{
this.fooService = fooService
}
}
I believe RegisterControllers(...)
is supposed to be used for prior versions of ASP.Net.