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
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:
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, IEnumerable
1 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(IEnumerable
1 parameters) +91
Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable1 parameters) +213
Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable
1 parameters) +248
Autofac.Core.Resolving.ResolveOperation.Resolve(IComponentRegistration registration, IEnumerable1 parameters) +52
Autofac.Core.Lifetime.LifetimeScope.Resolve(IComponentRegistration registration, IEnumerable
1 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... :(
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...