tags:

views:

436

answers:

2

Hi all, Has stated on the title, I'm trying to get autofac to resolve a type, that, as seen on the image bellow, seems registered.

Any thoughts on why the exception? Thanks autofac

UPDATE 1

Hi, I've moved to the latest build of the autofac. I'm registering the DemoService this way:

public class DependenciesModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var bin = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Remove(0, 6);
        var files = Directory.GetFiles(bin, "*.dll");

        foreach (var file in files)
        {
            var assembly = Assembly.LoadFile(file);
            builder.RegisterAssemblyTypes(assembly)
                .Where(t => typeof(IDependency).IsAssignableFrom(t))
                .AsImplementedInterfaces();
        }
    }
}

The container gets filled like so: alt text

And when I try to resolve the IDemoService I get:

Server Error in '/' Application.

The requested service 'MVCNHibernate.Services.IDemoService' has not been registered.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'MVCNHibernate.Services.IDemoService' has not been registered.

Source Error:

Line 17: var container = CreateHostContainer(registrations); Line 18: IContainerProvider containerProvider = new ContainerProvider(container); Line 19: ServiceLocator.SetLocator(t => containerProvider.RequestLifetime.Resolve(t)); Line 20: Line 21: var controllerBuilder = container.Resolve();

Source File: D:\Work\samples\MVCNHibernate\Core\AsurasStarter.cs Line: 19

Stack Trace:

[ComponentNotRegisteredException: The requested service 'MVCNHibernate.Services.IDemoService' has not been registered.] Autofac.ResolutionExtensions.Resolve(IComponentContext context, Service service, IEnumerable1 parameters) +165 Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable1 parameters) +64 Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType) +42 Core.<>c_DisplayClass1.b_0(Type t) in D:\Work\samples\MVCNHibernate\Core\AsurasStarter.cs:19 Core.Environment.ServiceLocator.Resolve() in D:\Work\samples\MVCNHibernate\Core\Environment\ServiceLocator.cs:19 MVCNHibernate.Controllers.HomeController..ctor() in D:\Work\samples\MVCNHibernate\MVCNHibernate\Controllers\HomeController.cs:52

[TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType) +0 System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType) +15 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +380 System.Reflection.ConstructorInfo.Invoke(Object[] parameters) +17 Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() +215 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) +416 Autofac.Core.Resolving.ComponentActivation.Activate(IEnumerable1 parameters) +91 Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable1 parameters) +213 Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable1 parameters) +248 Autofac.Core.Resolving.ResolveOperation.Resolve(IComponentRegistration registration, IEnumerable1 parameters) +52 Autofac.Core.Lifetime.LifetimeScope.Resolve(IComponentRegistration registration, IEnumerable1 parameters) +129 Autofac.ResolutionExtensions.TryResolve(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) +103 Autofac.ResolutionExtensions.TryResolve(IComponentContext context, Service service, Object& instance) +65 Autofac.Integration.Web.Mvc.AutofacControllerFactory.CreateController(RequestContext context, String controllerName) +199 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +118 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +46 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +63 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +13 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679426 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

UPDATE 2

Hi Peter,

I don't believe that's the problem, because if I "manually" register like:

builder.RegisterType<DemoService>().As<IDemoService>()

It gets resolved... :(

alt text

This is my DemoService:

public class DemoService : IDemoService
    {
        #region IDemoService Members

        public void DoSomething()
        {
        }

        #endregion
    }

UPDATE 3

public interface IDemoService : IDependency
    {
        void DoSomething();
    }

UPDATE 4 I Still haven't figure way this doesn't work. I've changed the way I register to:

builder.RegisterAssemblyTypes(assembly)
                .Where(t => typeof(IDependency).IsAssignableFrom(t))
                .AsImplementedInterfaces();

The container now haves the following registration:

[17] {Activator = DemoService (ReflectionActivator), Services = [MVCNHibernate.Services.IDemoService, Core.IDependency], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope} Autofac.Core.IComponentRegistration {Autofac.Core.Registration.ComponentRegistration}

What's blows my mind is that if I try to resolve by IDependency I get a instance, but if I try with the IDemoService I get an exception.

Any thoughts on why?

Thanks for the help...

A: 

I believe the error is related to an exception being thrown in the constructor of the DemoService class.

It is at least hinted to in the stacktrace:

TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle._InvokeConstructor(....

Update: the difference in your "real" code is that you register types with the following filter:

    builder.RegisterAssemblyTypes(assembly)
            .Where(t => typeof(IDependency).IsAssignableFrom(t))
            .AsImplementedInterfaces();

Could it be that the DemoService class never gets registered due to the fact that the class does not implement the IDependency interface?

Peter Lillevold
Hi Peter,The IDemoService interface inherits from the IDependency interface, as shown on my latest update to the question. Does it need to be a "direct" inheritance? Thanks
Bruno Shine
I've also updated the question like you suggested. Thanks for the tip :)
Bruno Shine
If I try to resolve with containerProvider.RequestLifetime.Resolve(typeof(IDependency)) it gets returned. Strange.... :(As shown on the second image on the question, both interfaces (IDemoService and IDependency) are set as the target services.
Bruno Shine
+2  A: 

Hi Bruno,

You should use Assembly.LoadFrom() instead of Assembly.LoadFile().

There are some details on the web about the difference between the two, e.g. http://blogs.msdn.com/suzcook/archive/2003/09/19/loadfile-vs-loadfrom.aspx and http://msdn.microsoft.com/en-us/library/dd153782.aspx

LoadFile() is the source of all sorts of crazy behaviour like you are seeing.

Hope this is the trick!

NB

Nicholas Blumhardt
Thanks Nicholas. That was it.... :)
Bruno Shine