Search code examples
c#nancytinyioc

Resolve concrete type with constructor arguments using TinyIoC


I have a Nancy API and have created a custom Bootstraper who inherits from DefaultNancyBootstrapper.

I also have the concrete type ConcreteFoo which i would like to bind to self in request scope and pass a specific constructor argument.

public class ConcreteFoo {
    private readonly int _baseInteger;
    public ConcreteFoo(int baseInteger) {
        _baseInteger = baseInteger;
    }
}

My custom Bootstraper is the following:

public class Bootstraper : DefaultNancyBootstrapper {
    protected override void ConfigureApplicationContainer(TinyIoCContainer container) {
        base.ConfigureApplicationContainer(container);
    }

    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) {
        base.ConfigureRequestContainer(container, context);
        container.Resolve<ConcreteFoo>(new NamedParameterOverloads {{"baseInteger", 100}});
    }
}

In order to resolve ConcreteFoo with the desired constructor parameter I have used the example provided in TinyIoC Wiki. I noticed that the code passes from the overrides in my custom bootstraper and if I write the below, the response is true.

var response = container.CanResolve<ConcreteFoo>(new NamedParameterOverloads {{"baseInteger", 100}});

However, I get an exception while the application is bootstraping. The inner-inner-inner... (lot's of inner) exception is listed below and states that it cannot create ConcreteFoo and that it's unable to resolve System.Int32.

InnerException: Nancy.TinyIoc.TinyIoCResolutionException
  HResult=-2146233088
  Message=Unable to resolve type: TestService.Api.ConcreteFoo
  Source=Nancy
  StackTrace:
       at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
       at Nancy.TinyIoc.TinyIoCContainer.MultiInstanceFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)
  InnerException: Nancy.TinyIoc.TinyIoCResolutionException
       HResult=-2146233088
       Message=Unable to resolve type: System.Int32
       Source=Nancy
       StackTrace:
            at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)
            at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, NamedParameterOverloads parameters, ResolveOptions options)
            at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)

I found this relevant question but it refers to registering an interface and not a concrete type.

Am I doing something wrong, or are you familiar with another way to resolve the concrete type and provide the constructor argument?


Solution

  • I think you need container.Register...

    Anyway, lately I prefer to use IRegistration classes, which are auto-wired by Nancy. That way the things are separated by topic/concern, and the bootstrapper is not modified at all.