1
votes

I am trying to use Nancy in a console hosted Owin app. My Bootstrapper and the main startup of Nancy are defined as follows:

//NOTE: kernel is a static variable and is the ninject StandardKernel

    public class Bootstrapper : NinjectNancyBootstrapper
    {
        protected override IKernel GetApplicationContainer()
        {
            kernel.Load<FactoryModule>();
            return kernel;
        }

        protected override void ConfigureConventions(NancyConventions nancyConventions)
        {
            base.ConfigureConventions(nancyConventions);
            nancyConventions.StaticContentsConventions.Add(
                StaticContentConventionBuilder.AddDirectory("scripts", @"scripts", "js")
                );
            nancyConventions.StaticContentsConventions.Add(
                StaticContentConventionBuilder.AddDirectory("css", @"css", "css")
                );
        }

        protected override void ApplicationStartup(IKernel container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);
        }


        protected override IKernel CreateRequestContainer(Nancy.NancyContext context)
        {
            return kernel;
        }
    }


class Program
{  
        public static void Main(string[] args)
       {        
        var options = new StartOptions();
        options.Urls.Add(this.config.HostUrl.AbsoluteUri);
        using(WebApp.Start(options,
            builder =>
            {
                builder.Properties["host.AppName"] = "Self host";
                builder.Properties["host.AppMode"] = "development";
                builder.MapSignalR("/signalr",
                    new HubConfiguration
                    {
                        EnableJSONP = true,
                        EnableJavaScriptProxies = true,
                        Resolver = new DefaultDependencyResolver()
                    });
                builder.UseErrorPage();
                builder.UseNancy();
            })
          { Console.ReadLine();}
      }
    }

The call to UseNancy() throws the following error. Ninject is not able to resolve IRouteResolverTrie.

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=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.
  Source=Nancy
  StackTrace:
       at Nancy.Bootstrapper.NancyBootstrapperBase`1.SafeGetNancyEngineInstance()
       at Nancy.Bootstrapper.NancyBootstrapperBase`1.GetEngine()
       at Nancy.Owin.NancyMiddleware.UseNancy(NancyOptions options)
       at Owin.AppBuilderExtensions.UseNancy(IAppBuilder builder, NancyOptions options)
       at MyHost.<Main>b__b(IAppBuilder builder) in c:\dev\MyHost\Program.cs:line 194
       at Microsoft.Owin.Hosting.Engine.HostingEngine.ResolveApp(StartContext context)
       at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
       at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options, Action`1 startup)
       at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options, Action`1 startup)
       at MyHost.Main() in c:\dev\MyHost\Program.cs:line 177
       at MyHost.Main(String[] args) in c:\dev\MyHost\Program.cs:line 248
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Ninject.ActivationException
       HResult=-2146233088
       Message=Error activating IRouteResolverTrie
No matching bindings are available, and the type is not self-bindable.
Activation path:
  4) Injection of dependency IRouteResolverTrie into parameter trie of constructor of type DefaultRouteResolver
  3) Injection of dependency IRouteResolver into parameter routeResolver of constructor of type DefaultRequestDispatcher
  2) Injection of dependency IRequestDispatcher into parameter dispatcher of constructor of type NancyEngine
  1) Request for INancyEngine

Suggestions:
  1) Ensure that you have defined a binding for IRouteResolverTrie.
  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
  3) Ensure you have not accidentally created more than one kernel.
  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
  5) If you are using automatic module loading, ensure the search path and filters are correct.

       Source=Ninject
       StackTrace:
            at Ninject.KernelBase.Resolve(IRequest request)
            at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
            at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
            at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
            at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
            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 Ninject.Activation.Providers.StandardProvider.Create(IContext context)
            at Ninject.Activation.Context.ResolveInternal(Object scope)
            at Ninject.Activation.Context.Resolve()
            at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
            at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
            at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
            at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
            at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
            at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
            at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
            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 Ninject.Activation.Providers.StandardProvider.Create(IContext context)
            at Ninject.Activation.Context.ResolveInternal(Object scope)
            at Ninject.Activation.Context.Resolve()
            at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
            at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
            at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
            at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
            at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
            at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
            at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
            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 Ninject.Activation.Providers.StandardProvider.Create(IContext context)
            at Ninject.Activation.Context.ResolveInternal(Object scope)
            at Ninject.Activation.Context.Resolve()
            at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
            at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
            at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
            at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
            at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
            at Nancy.Bootstrappers.Ninject.NinjectNancyBootstrapper.GetEngineInternal()
            at Nancy.Bootstrapper.NancyBootstrapperBase`1.SafeGetNancyEngineInstance()
       InnerException: 

Following is a list of the packages installed:

<packages>
  <package id="jQuery" version="2.1.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Cors" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.SignalR.Owin" version="1.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.SignalR.SelfHost" version="2.2.0" targetFramework="net45" />
  <package id="Microsoft.Owin" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Cors" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Diagnostics" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Host.HttpListener" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Hosting" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.Security" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Owin.SelfHost" version="3.0.0" targetFramework="net45" />
  <package id="Nancy" version="1.0.0" targetFramework="net45" />
  <package id="Nancy.Bootstrappers.Ninject" version="0.23.2" targetFramework="net45" />
  <package id="Nancy.Hosting.Self" version="0.23.2" targetFramework="net45" />
  <package id="Nancy.Owin" version="1.0.0" targetFramework="net45" />
  <package id="Nancy.Viewengines.Razor" version="0.23.2" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
  <package id="Ninject" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.Extensions.ChildKernel" version="3.2.0.0" targetFramework="net45" />
  <package id="ninject.extensions.conventions" version="3.2.0.0" targetFramework="net45" />
  <package id="Owin" version="1.0" targetFramework="net45" />
</packages>
1

1 Answers

2
votes

I haven't used Ninject, only StructureMap, but I believe their setup is the same. This is how I set Nancy up when using it as middleware.

public class Bootstrapper : NinjectNancyBootstrapper
{
    private readonly IKernel  _kernel;

    public Bootstrapper(IKernel  kernel)
    {
        _kernel = kernel;
    }

    protected override IKernel GetApplicationContainer()
    {
        return _kernel;
    }
}
using (WebApp.Start(options, builder =>
{
    var kernel = SetupNinject();

    builder.UseNancy(options =>
    {
        options.Bootstrapper = new Bootstrapper(kernel);
    });
})
{
    Console.ReadLine();
}

I also notice that not all of your Nancy dependencies are v1.0, make sure they're all up to date because there are numerous breaking changes between v0.23 and v1.0 in both the routing and bootstrappers which could cause your app to not load.