dotnet コアで Nancy を使用して動作する JWT でステートレス認証を取得しようとしています。これをUbuntu VMで実行しています。しかし、私は私のでcannot resolve type
エラーに遭遇Startup.cs
し続けます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 拡張機能:
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;
}
}
ホームモジュール:
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 コア初心者ですが、この問題を解決する方法を誰か説明してもらえますか?
編集、作業コード:
ブートストラップの変更:
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));
});
}
サイト ノート: おそらくを be of typeに変更Configuration
し、上記のように 2 回ではなく、-のコンストラクターで 1 回だけ型キャストを行います。Startup.cs
ConfigurationRoot
Startup.cs
Configure