Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型

时间:2016-10-21 03:25:14

标签: c# .net nancy tinyioc .net-core

我一直在尝试使用JWT在dotnet核心上使用Nancy来获得无状态身份验证。我在Ubuntu VM上运行它。但我cannot resolve type Startup.cs的{​​{1}}错误导致x.UseNancy();错误

我的启动课看起来像这样:

public class Startup
{
   public static IConfigurationRoot Configuration;



   public Startup(IHostingEnvironment env)
   {
        Configuration = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables()
            .Build();
   }

    public void Configure(IApplicationBuilder app)
    {
        app.UseOwin(x =>
        {
            x.RequiresStatelessAuth(
                new MySecureTokenValidator(new ConfigProvider(Configuration)),
                new StatelessAuthOptions() { IgnorePaths = new List<string>(new[] { "/login", "/content" }) });
            x.UseNancy();
        });
    }
}

我的ConfigProvider类:

public class ConfigProvider : IConfigProvider
{
    private readonly IConfigurationRoot config;

    public ConfigProvider(IConfigurationRoot config)
    {
        this.config = config;
    }

    public string GetAppSetting(string propertyName)
    {
        return config.GetSection($"AppSettings:{propertyName}").ToString();
    }
}

RequiresStatelessAuth Extension:

using MidFunc = System.Func<System.Func<System.Collections.Generic.IDictionary<string, object>,
    System.Threading.Tasks.Task>, System.Func<System.Collections.Generic.IDictionary<string, object>,
    System.Threading.Tasks.Task>>;

public static class StatelessAuthExtensions
{
    public static Action<MidFunc> RequiresStatelessAuth(this Action<MidFunc> builder, ITokenValidator tokenValidator, StatelessAuthOptions options = null)
    {
         builder(next => new StatelessAuth(next, tokenValidator, options).Invoke);
         return builder;
    }
}

HomeModule:

public class HomeModule : NancyModule
{
    public HomeModule(IConfigProvider configProvider, IJwtWrapper jwtWrapper)
    {
        //some code here
    }
}

我没有在IoC容器上注册任何内容。但我在我的引导程序中尝试了以下内容:

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    base.ConfigureApplicationContainer(container);
    container.Register<IConfigProvider, ConfigProvider>();
    container.Register<ConfigurationRoot>();
}

为了实现这一点,我在应用程序中将IConfigurationRoot更改为ConfigurationRoot

无论我尝试过什么,我都会遇到同样的错误。堆栈跟踪:

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.NancyEngine
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRequestDispatcher
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRouteResolver
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.RouteCache
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: nancyTest.HomeModule
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: ConfigProvider
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Microsoft.Extensions.Configuration.ConfigurationRoot
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Microsoft.Extensions.Configuration.ConfigurationRoot
 ---> Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: System.Collections.Generic.IList`1[[Microsoft.Extensions.Configuration.IConfigurationProvider, Microsoft.Extensions.Configuration.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.MultiInstanceFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.MultiInstanceFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetFactoryForChildContainer(Type type, TinyIoCContainer parent, TinyIoCContainer child)\n
   at Nancy.TinyIoc.TinyIoCContainer.GetParentObjectFactory(TypeRegistration registration)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\n
   at System.Linq.Enumerable.<CastIterator>d__29`1.MoveNext()\n
   at Nancy.Routing.RouteCache.BuildCache(IEnumerable`1 modules)\n
   at Nancy.Routing.RouteCache..ctor(INancyModuleCatalog moduleCatalog, INancyContextFactory contextFactory, IRouteSegmentExtractor routeSegmentExtractor, IRouteDescriptionProvider routeDescriptionProvider, ICultureService cultureService, IEnumerable`1 routeMetadataProviders)\n
   at lambda_method(Closure , Object[] )\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n  
   --- End of inner exception stack trace ---\n
   at Nancy.TinyIoc.TinyIoCContainer.ConstructType(Type requestedType, Type implementationType, ConstructorInfo constructor, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.SingletonFactory.GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.ResolveInternal(TypeRegistration registration, NamedParameterOverloads parameters, ResolveOptions options)\n
   at Nancy.TinyIoc.TinyIoCContainer.Resolve[ResolveType]()\n
   at Nancy.Bootstrapper.NancyBootstrapperBase`1.SafeGetNancyEngineInstance()}

我是Nancy / Linux / dotnet的核心菜鸟,有人可以解释如何解决这个问题吗?

编辑,工作代码:

Bootstrapper更改:

public class CustomBootstrapper : DefaultNancyBootstrapper
{
    private ConfigurationRoot config;

    public CustomBootstrapper(ConfigurationRoot config)
    {
        this.config = config;
    }

    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);
        container.Register<ConfigurationRoot>(config);
    }

    //more code
}

Startup.cs:

public void Configure(IApplicationBuilder app)
{
    app.UseOwin(x =>
    {
        x.RequiresStatelessAuth(
            new MySecureTokenValidator(new ConfigProvider((ConfigurationRoot)Configuration)),
            new StatelessAuthOptions() { IgnorePaths = new List<string>(new[] { "/login", "/content" }) });
        x.UseNancy(opts => opts.Bootstrapper = new CustomBootstrapper((ConfigurationRoot)Configuration));
    });
}

网站注意:我可能会将Configuration中的Startup.cs更改为ConfigurationRoot类型,然后在Startup.cs的构造函数中执行一次类型转换 - 而不是如上Configure中的两倍。

0 个答案:

没有答案