0

I'm planning to auto-wire the interfaces and repositories in my project, both BL and DAL. but I'm encountering an issue which I have no idea on how to solve it.

Here is the code

namespace MovieManager.UI
{
    public static class ContainerConfig
    {
        public static IContainer Configure()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetAssembly(typeof(HomeController))).InstancePerRequest();
            builder.RegisterControllers(Assembly.GetAssembly(typeof(MovieController))).InstancePerRequest();

            builder.RegisterAssemblyTypes(Assembly.Load(nameof(BL)))
                .Where(t => t.Namespace != null && t.Namespace.Contains("Repositories"))
                .As(t => t.GetInterfaces().FirstOrDefault(i => i.Name == "I" + t.Name));

            builder.RegisterAssemblyTypes(Assembly.Load(nameof(DAL)))
                .Where(t => t.Namespace != null && t.Namespace.Contains("Repositories"))
                .As(t => t.GetInterfaces().FirstOrDefault(i => i.Name == "I" + t.Name));

            //builder.RegisterType<MovieSupervisor>().As<IMovieSupervisor>();
            //builder.RegisterType<GenreTransactionSupervisor>().As<IGenreTransactionSupervisor>();


            //builder.RegisterType<MovieRepository>().As<IMovieRepository>();
            //builder.RegisterType<GenreRepository>().As<IGenreRepository>();
            //builder.RegisterType<GenreTransactionRepository>().As<IGenreTransactionRepository>();

            var containerBuilder = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(containerBuilder));
            return containerBuilder;
        }
    }
}

And this is my project folder structure

enter image description here

and getting this error

enter image description here

What i'm doing wrong. Any help would be appreciated.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76

2 Answers2

1

This isn't an Autofac issue, it's most likely just that the assembly cannot be loaded because the name is wrong.

The code nameof(BL) will explicitly output the string "BL". I imagine that your assembly is actually called MovieManager.BL based on the project name, hence the failure.

Note that nameof(MovieManager.BL) will also output "BL", because nameof outputs the last component of the name you provide.

You can either pass the full name manually, or, if you have the assembly referenced, you can just take the same approach as you have with the controllers, and get the assembly from the type.

Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
  • Thanks for response. But if I change the `Assembly.Load(nameof(BL))` to `Assembly.Load("MovieManager.BL")`. I'm getting a new error near `builder.Build()` as `System.ArgumentNullException: 'Value cannot be null. Parameter name: serviceType'` – Ranjith Varatharajan Jun 04 '20 at 04:40
  • That's the filter where you're doing string concatenation causing problems. However, the issue you originally presented does appear solved; the assembly is found. Take some time to work through the LINQ statement and solve that null reference issue, and if you still can't figure it out, please open a new question with an updated stack trace and repro info. – Travis Illig Jun 04 '20 at 16:14
  • 'System.ArgumentNullException: 'Value cannot be null. Parameter name: serviceType' I got the above error because some of the Repositories didn't have their own interfaces (With your code it find the repository and it's interface. If any thing missed you will get value can not be null error) – Thinira Jun 04 '21 at 19:39
1

The problem is in the second part of the registration: .As(t => t.GetInterfaces().FirstOrDefault(i => i.Name == "I" + t.Name))

Your filter on first or default probably returns null.

Please check if the service name and interface matches to your criteria in FirstOrDefault

Also, as mentioned before in my comment the Assembly.Load needs to have a correct Assembly name but you fixed that already.

bluedot
  • 628
  • 8
  • 24