Autofac MVC DI

0

Czołem koledzy programiści!
Mam solucję podzieloną na kilka projektów. Tam gdzie to potrzebne, dodaję klasę dziedziczącą po Autofac.Module. Chciałbym załadować wszystkie moduły do projektu startowego (Web) bez dodawania referencji do wszystkich pozostałych projektów.

Kod konfiguracji, który oczywiście startuję w Global.asax

private static void SetAutofacContainer()
 {
            var builder = new ContainerBuilder();

            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            builder.RegisterAssemblyModules(loadedAssemblies);
           
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

Przykładowy moduł w osobnym projekcie

 public class ApplicationModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(ThisAssembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces()
            .AsSelf()
            .InstancePerLifetimeScope();
        }
    }

Niestety otrzymuję błąd

`Inner Exception 1:
DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = CatalogController (ReflectionActivator), Services = [MySolution.Web.Controllers.CatalogController], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope

Inner Exception 2:
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MySolution.Web.Controllers.CatalogController' can be invoked with the available services and parameters:
Cannot resolve parameter 'MySolution.Contracts.Interfaces.ICatalogViewModelService catalogViewModelService' of constructor 'Void .ctor(MySolution.Contracts.Interfaces.ICatalogViewModelService)'.`

Co czynię źle i czy mógłby ktoś naprowadzić mnie, jak to wszystko powiązać?
Pozdrawiam,
KK

0

Jeśli rejestrujsze coś jako AsImplementedInterface to po co Ci jeszcze AsSelf. To jest cały kod? Bo powinieneś zarejestrować controllery też.

Na swoim githubie mam coś podobnego ale wszystkie moduły mam w jednym projekcie
https://github.com/szydlaczek/CarRental/blob/master/CarRental.Web/Global.asax.cs

0

To chyba nie cały kod. Jesli dobrze zrozumiałem : spróbuj stworzyć jeden lub więcej modułów link projektów, które reference do wszystkich lub grupy bibliotek i stamtąd zrób registrację. Ponadto jesli Twoja strona nie uzywa serwisów to nie ma sensu czegokolwiek stamtąd rejetrować. One powinny być zarejestrowane w tasks project.

1 użytkowników online, w tym zalogowanych: 0, gości: 1