I'm new to Autofac and I want to have a better understanding with the following exception. Any help/pointers on this would be highly appreciated!
Getting following Autofac.Core.DependencyResolutionException
.
Unhandled Exception: Autofac.Core.DependencyResolutionException: None of the constructors found with 'Public binding flags' on type 'Test.Authorization.LoginService' can be invoked with the available services and parameters: Cannot resolve parameter 'System.Func`1[System.IO.Stream] readStream' of constructor 'Void .ctor(System.Func`1[System.IO.Stream], System.Func`1[System.IO.Stream])'. at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Activators\Reflection\ReflectionActivator.cs:line 117 at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 79 at Autofac.Core.Resolving.InstanceLookup.Execute() in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 62 at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\ResolveOperation.cs:line 124 at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 115 at Autofac.Features.Collections.CollectionRegistrationSource.c__DisplayClass4.c__DisplayClass6.b__1(IComponentRegistration cr) in \autofac\src\Source\Autofac\Features\Collections\CollectionRegistrationSource.cs:line 80 at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at Autofac.Features.Collections.CollectionRegistrationSource.c__DisplayClass4.b__0(IComponentContext c, IEnumerable`1 p) in \autofac\src\Source\Autofac\Features\Collections\CollectionRegistrationSource.cs:line 80 at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Activators\Delegate\DelegateActivator.cs:line 65 at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 79 at Autofac.Core.Resolving.InstanceLookup.Execute() in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 62 at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\ResolveOperation.cs:line 124 at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 115 at Autofac.Core.Activators.Reflection.AutowiringParameter.c__DisplayClass2.b__0() in \autofac\src\Source\Autofac\Core\Activators\Reflection\AutowiringParameter.cs:line 51 at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() in \autofac\src\Source\Autofac\Core\Activators\Reflection\ConstructorParameterBinding.cs:line 113 at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Activators\Reflection\ReflectionActivator.cs:line 119 at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 79 at Autofac.Core.Resolving.InstanceLookup.b__0() in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 64 at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator) in \autofac\src\Source\Autofac\Core\Lifetime\LifetimeScope.cs:line 269 at Autofac.Core.Resolving.InstanceLookup.Execute() in \autofac\src\Source\Autofac\Core\Resolving\InstanceLookup.cs:line 64 at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\ResolveOperation.cs:line 124 at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\ResolveOperation.cs:line 69 at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Resolving\ResolveOperation.cs:line 96 at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Lifetime\LifetimeScope.cs:line 235 at Autofac.Core.Container.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\Core\Container.cs:line 191 at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) in \autofac\src\Source\Autofac\ResolutionExtensions.cs:line 728 at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\ResolutionExtensions.cs:line 346 at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\ResolutionExtensions.cs:line 244 at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters) in \autofac\src\Source\Autofac\ResolutionExtensions.cs:line 197 at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, Parameter[] parameters) in \autofac\src\Source\Autofac\ResolutionExtensions.cs:line 213
Here's my cs code:
public sealed class AuthorizationModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder
.Register(LoginService)
.AsImplementedInterfaces()
.SingleInstance();
}
static LoginService LoginService(IComponentContext c)
{
var file = c.Resolve<IFile>();
var dataPath = <Some Path>;
return new LoginService
(readStream: () => file.Open(dataPath),
writeStream: () => file.Create(dataPath));
}
}
sealed class LoginService : TLoginService.Iface, IService
{
readonly Func<Stream> _readStream;
readonly Func<Stream> _writeStream;
readonly XmlSerializer _serializer;
public LoginService(
Func<Stream> readStream,
Func<Stream> writeStream
)
{
Contract.Requires(readStream != null);
Contract.Requires(writeStream != null);
_readStream = readStream;
_writeStream = writeStream;
_serializer = new XmlSerializer(typeof (List<LoginRecord>));
}
...
...
Your LoginService
class has a constructor similar to LoginService(Func<System.IO.Stream> stream)
. Autofac is unable to create the Func<System.IO.Stream>
argument. So register it appropriately so that Autofac can create the LoginService
.
Eg:
builder.Register(c => new LoginService(d => File.Open("Foo));
Go through Component Registration section of the Wiki to find which method suits you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With