Search code examples
c#.netinterfacenancybootstrapping

NancyFx passing an Interface to a module constructor results in error


When I pass an interface into my module constructor it results in the error: Unable to resolve type: System.Web.HttpContextBase. Below is the Module code:

public class LeaderboardModule : NancyModule
{
    private readonly IUserService _userService;

    public LeaderboardModule(IUserService userService)
    {
        _userService = userService;

        Get["/"] = _ => View["Index"];

        Get["/NewUser"] = _ => View["NewUser.cshtml"];

        Post["/NewUser"] = _ => View["NewUser.cshtml"];

        Get["/UserList"] = _ =>
        {
            var userList = _userService.GetUsers();

            return View["UserList.cshtml", userList];
        };
    }
}

And here is the stack trace:

[TinyIoCResolutionException: Unable to resolve type: System.Web.HttpContextBase]
    Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration,     NamedParameterOverloads parameters, ResolveOptions options) +1940
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType,     ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +774

[TinyIoCResolutionException: Unable to resolve type: Metro.Leaderboard.Data.JsonService]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType,   ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +965
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container,   NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +225
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType,     ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +774

[TinyIoCResolutionException: Unable to resolve type: Metro.Leaderboard.Business.UserService]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +965
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container,    NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.SingletonFactory.GetFactoryForChildContainer(Type type, TinyIoCContainer parent,   TinyIoCContainer child) +100
   Nancy.TinyIoc.TinyIoCContainer.GetParentObjectFactory(TypeRegistration registration) +141
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration,   NamedParameterOverloads parameters, ResolveOptions options) +712
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +774

[TinyIoCResolutionException: Unable to resolve type:     Metro.Leaderboard.Web.Modules.LeaderboardModule]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +965
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +225
   Nancy.TinyIoc.TinyIoCContainer.<ResolveAllInternal>b__43(TypeRegistration registration) +91
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
   System.Linq.<CastIterator>d__b1`1.MoveNext() +85
   Nancy.Routing.RouteCache.BuildCache(IEnumerable`1 modules) +514
   Nancy.Routing.RouteCache..ctor(INancyModuleCatalog moduleCatalog, INancyContextFactory   contextFactory, IRouteSegmentExtractor routeSegmentExtractor, IRouteDescriptionProvider   routeDescriptionProvider, ICultureService cultureService) +189
   lambda_method(Closure , Object[] ) +221
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType,  ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +1100

    [TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.RouteCache]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +1178
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +225
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +774

[TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRouteResolver]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +965
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +225
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +774

[TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRequestDispatcher]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +965
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +225
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +774

[TinyIoCResolutionException: Unable to resolve type: Nancy.NancyEngine]
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options) +965
   Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, ResolveOptions options) +99
   Nancy.TinyIoc.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options) +213
   Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options) +225
   Nancy.TinyIoc.TinyIoCContainer.Resolve(Type resolveType) +106
   Nancy.TinyIoc.TinyIoCContainer.Resolve() +112
   Nancy.DefaultNancyBootstrapper.GetEngineInternal() +56
   Nancy.Bootstrapper.NancyBootstrapperBase`1.SafeGetNancyEngineInstance() +61

[InvalidOperationException: Something went wrong when trying to satisfy one of the dependencies  during composition, make sure that you've registered all new dependencies in the container and      inspect the innerexception for more details.]
   Nancy.Bootstrapper.NancyBootstrapperBase`1.SafeGetNancyEngineInstance() +135
   Nancy.Bootstrapper.NancyBootstrapperBase`1.GetEngine() +113
   Nancy.Hosting.Aspnet.NancyHttpRequestHandler..cctor() +65

[TypeInitializationException: The type initializer for 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler' threw an exception.]
   Nancy.Hosting.Aspnet.NancyHttpRequestHandler..ctor() +0

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
    System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
    System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
    System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) +1136
    System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +128
    System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) +18
    System.Web.HttpRuntime.CreateNonPublicInstance(Type type, Object[] args) +60
    System.Web.Configuration.HandlerFactoryCache..ctor(String type) +46
    System.Web.HttpApplication.GetFactory(String type) +86
    System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +262
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

My initial thought is that it's an issue with the Nancy bootstrapper, so I decided to use StructureMap and I'm still getting the same error! Any help on this would be greatly appreciated


Solution

  • The stack trace tells you everything you need to know - your module has a dependency on UserService, and UserService has a dependency on JsonService, which has a dependency on HttpContextBase, which it can't resolve (because you shouldn't be using it really, or if you do, you certainly shouldn't have it as a constructor dependency)