Search code examples
c#castle-windsor

Could not resolve non-optional dependency


I have an interface like this:

public interface IdFactory
{
     Id Create(Guid userId);
}

and the Castle Windsor definition is like this:

container.Register(Castle.MicroKernel.Registration
    .Component.For<Id>().ImplementedBy<Id>().LifeStyle.Transient);

The calling code looks like this:

 var ID = IDFactory.Create(new Guid(userId));

The error I get is:

Castle.MicroKernel.Resolvers.DependencyResolverException: 'Could not resolve non-optional dependency for 'Id' (Id). Parameter 'id' type 'System.Guid''

What is the problem here? Do I have to configure System.Guid in Castle Windsor?


Solution

  • Parameter names of factory method and of class constructor being resolved must be the same:

    public class Id
    {
        public Id(Guid userId /*not - Guid id*/)
        {
        }
    }