0

독립형 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; 
    } 
} 

답변

0

startup.cs 먼저 호출됩니다. 그리고 Startup.cs에서 Unity를 등록하지 않는 것을보십시오.
즉, 구체적인 구현을 찾을 수 없습니다. 그것을 사용하기 전에 등록하려고합시다.

+0

아직도 운이 없어요. 이러한 변경 사항을 반영하기 위해 질문 코드를 업데이트했습니다. – Tomuke

+0

다음과 같이 해봅시다. HttpConfiguration config = new HttpConfiguration(); config.DependencyResolver = new UnityDependencyResolver ( SelfHostWebApiOwin.UnityHelpers.GetConfiguredContainer()); SelfHostWebApiOwin.UnityHelpers.GetConfiguredContainer()는 컨테이너입니다. 작동하는지 알려주세요. – tlp

관련 문제