1

I'm completely new in DI so i created a new project to understand how it works in a simple project. I have BLL project, which only have

public interface IService
{
   string Method();
}

internal class SomeService : IService
{
   public string Method()
   {
       return "Hi from BLL";
   }
} 

class BllModule : Autofac.Module
{
   protected override void Load(ContainerBuilder builder)
   {
       builder.RegisterAssemblyTypes((Assembly.GetExecutingAssembly())
                     .Where(t => t.Name.EndsWith("Service"))
                     .AsImplementedInterfaces()
                     .InstancePerLifetimeScope();
   }
}

I want to resolve this Service in UI project.

class Program
{
    static void Main(string[] args)
    {
        //Autofac Configuration
        var builder = new ContainerBuilder();
        builder.RegisterModule(new BllModule());

        var container = builder.Build();
        // here i check if container contatins my type (just for debugging), so it has Service BLL.IService
        var s = container.ComponentRegistry.Registrations.ToList();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        using (var scope = container.BeginLifetimeScope())
        {
            // so here i have my exception
            var service = scope.Resolve<IService>();
            service.Method();
        }
    }    
}

So code in using block throws

Autofac.Core.Registration.ComponentNotRegisteredException: 
"The requested service 'BLL.IService' has not been registered." 

I'have updated code so now it work's

Nicefsf
  • 140
  • 1
  • 1
  • 9
  • This does not answer your question, but i hope it helps -> https://stackoverflow.com/questions/59164555/autofac-automate-assembly-scanning-net-core-3-0-register-according-to-lifetim – panoskarajohn Jan 22 '20 at 15:31
  • @panoskarajohn it makes a lot of sence moving module classes to BLL, but it didn't work for me – Nicefsf Jan 22 '20 at 15:49
  • **Try my code**, you should not use `builder.RegisterAssemblyTypes(Assembly.LoadFile("E:\\Test\\3 layer app\\BLL\\bin\\Debug\\BLL.dll"))` to the best of my knowledge. Move your module to the BLL and do not use the `Assembly.LoadFile` – panoskarajohn Jan 22 '20 at 15:56
  • @panoskarajohn Thanks, it's helped me, for some reason loading via `Assembly.LoadFile` not working for this example.I think you should answer, not comment my question so i can mark it as a correct answer. Also, i'm still not understanding how can i call an implementation of my interface (SomeService), so maybe you can help me with it too – Nicefsf Jan 22 '20 at 16:11
  • Thanks but it's ok, it was not an answer. Try looking at the docs. Also take a look here -> https://www.c-sharpcorner.com/article/using-asimplementedinterfaces/, https://stackoverflow.com/questions/41542079/what-does-asself-do-in-autofac – panoskarajohn Jan 22 '20 at 16:17
  • If you feel still confused edit this question or ask a new one, and show the parts that do not make sense. I am glad i helped. – panoskarajohn Jan 22 '20 at 16:18
  • @panoskarajohn thanks a lot now i think i got it and can implement in project – Nicefsf Jan 22 '20 at 16:32

0 Answers0