私はスタンドアロンWebAPI(クライアントなし)を作成し、WebAPIのUnityコンテナを使用してDIを実装しようとしました。Unity WebAPI:個々のタイプを解決できません
ただし、(カスタムトークン認証サービスに依存する)OAuthServerOptionsでプロバイダを指定する場合、解決する必要があるものを手動で指定することはできません。
私が間違っている場所を指摘できる人は、MVCを持つUnityと少し違って動作するようです。
ユニティ設定:
.RegisterComponents()
はStartup.csに呼ばれています。
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// These two are the ones which won't resolve!
container.RegisterType<IAuthenticationTokenProvider, SimpleRefreshTokenProvider>();
container.RegisterType<IOAuthAuthorizationServerProvider, SimpleAuthorizationServerProvider>();
container.RegisterType<ITokenAuthenticationManager, TokenAuthenticationManager>(new HierarchicalLifetimeManager());
// Some more type registrations which work fine...
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
Startup.Auth:私は手動でプロバイダを解決することができません場所です
...
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthServerOptions { get; private set; }
public void ConfigureAuth(IAppBuilder app)
{
// Some OWIN context setups...
// Auth server setup
OAuthServerOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IOAuthAuthorizationServerProvider)),
RefreshTokenProvider = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IAuthenticationTokenProvider)),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(timeSpan.TotalMinutes + 5),
AllowInsecureHttp = true
};
// Enable bearer tokens...
}
}
スタートアップ:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
UnityConfig.RegisterComponents();
ConfigureAuth(app);
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);
}
}
SimpleAuthorizationServiceProvider:
// Note: OAuthAuthoizationServiceProvider implements IOAuthAuthorizationServerProvider
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private ITokenAuthenticationManager tokenAuthenticationManager;
public SimpleAuthorizationServerProvider(ITokenAuthenticationManager tokenAuthenticationManager)
{
this.tokenAuthenticationManager = tokenAuthenticationManager;
}
}
SimpleRefreshTokenProvider:あなたが知っているように
public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
{
private ITokenAuthenticationManager tokenAuthenticationManager;
public SimpleRefreshTokenProvider(ITokenAuthenticationManager tokenAuthenticationManager)
{
this.tokenAuthenticationManager = tokenAuthenticationManager;
}
}
まだ運がありません私は恐れています。これらの変更も反映するように質問コードを更新しました。 – Tomuke
これで試してみましょう:HttpConfiguration config = new HttpConfiguration(); config.DependencyResolver = new UnityDependencyResolver( SelfHostWebApiOwin.UnityHelpers.GetConfiguredContainer()); SelfHostWebApiOwin.UnityHelpers.GetConfiguredContainer()はあなたのコンテナです。それが動作すれば教えてください – tlp