Unity WebAPI:无法解析个别类型

时间:2016-07-02 14:02:44

标签: c# asp.net-web-api2 inversion-of-control unity-container

我创建了一个独立的WebAPI(没有客户端),并尝试使用Unity容器为WebAPI开始实现DI。

但是,在OAuthServerOptions中指定我的提供程序(依赖于自定义令牌身份验证服务)时,我无法手动指定应解析的内容。

如果有人可以指出我哪里出错了,因为它看起来与Unity与MVC有点不同。

Unity配置:

在Startup.cs中调用

.RegisterComponents()

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;
    }
}

1 个答案:

答案 0 :(得分:0)

如您所知,将首先调用startup.cs。我发现你没有在Startup.cs中注册Unity 这意味着你找不到具体的实现。让我们在使用之前尝试注册。