1

웹 API로 정책 삽입을 구현 중이며 DI의 경우 사용자 지정 DependancyResolver를 사용하고 있습니다. InterfaceIntercept 접근법을 사용하여 정책 삽입을 구현했습니다. Class (사용자 정의 클래스)의 경우에는 정상적으로 작동하지만 ApiController의 경우 Policy Injection이 실행되지 않습니다.WebAPI 컨트롤러에서 정책 주입 차단이 작동하지 않습니다.

APIController에서 Policy Injection을 호출하기 위해 컨트롤러와 함께 구현 된 & 인터페이스를 만들었습니다. 아래 코드 공유 : 또한 MessageHandlers와 정책을 호출해야합니다.

정책 사출 코드 :

public class LogExceptionsCallHandler : ICallHandler 
{ 

    public IMethodReturn Invoke(IMethodInvocation input, 
           GetNextHandlerDelegate getNext) 
    { 
     IApplicationLogger logger = new ApplicationLogger(); 
     logger.Log(LoggingLevel.TRACE, "Entering " + input.MethodBase.DeclaringType.FullName + "." + input.MethodBase.Name); 
     //// Perform the operation 
     var methodReturn = getNext().Invoke(input, getNext); 
     //// Method failed, go ahead 
     if (methodReturn.Exception != null) 
      return methodReturn; 
     //// If the result is negative, then throw an exception 
     logger.Log(LoggingLevel.TRACE, "Ending " + input.MethodBase.DeclaringType.FullName + "." + input.MethodBase.Name); 

     return methodReturn; 
    } 
    public int Order { get; set; } 
} 

속성 코드

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method)] 
public class LogExceptionsAttribute : HandlerAttribute 
{ 
    public LogExceptionsAttribute() 
    { 
    } 
    public HandleLogging HandleLogging { get; set; } 
    public int RetryCount { get; set; } 
    public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container) 
    { 
     return new LogExceptionsCallHandler(); 
    } 
} 

인터페이스 코드 :이 인터페이스는 ApiController에 의해 구현되고

[LogExceptions] 
public interface IRequestExecutionController : IHttpController 
{ 
    [LogExceptions] 
    HttpResponseMessage ExecuteRequest(); 
} 

IRequestExecutionController 인터페이스가 RequestExecutionController에 의해 구현 중입니다. 단결과 등록 유형 :

container.RegisterType<IDDNPublicAPI.PassThrough.IRequestExecutionController, RequestExecutionController>("requestexecution"); 

우리가 의존성을 해결하기 위해 단결을 가지고, 그래서 우리는 컨트롤러 인스턴스 생성을 처리하는 컨트롤러 팩토리 클래스를 만들었습니다으로 차단

container.Configure<Interception>().SetInterceptorFor<IDDNPublicAPI.PassThrough.IRequestExecutionController>(new InterfaceInterceptor()); 

등록.

public class UnityControllerFactory : IHttpControllerActivator 
{ 
    private IUnityContainer container; 

    private readonly IControllerFactory innerFactory; 

    public UnityControllerFactory(IUnityContainer container) 
     : this(container, new DefaultControllerFactory()) 
    { } 

    public UnityControllerFactory(IUnityContainer container, IControllerFactory innerFactory) 
    {`enter code here` 
     this.container = container; 
     this.innerFactory = innerFactory; 
    }enter code here 

    public IHttpController Create(HttpRequestMessa`enter code here`ge request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 
    {  
     var controller = (IHttpController)this.container.Resolve(controllerType, controllerDescriptor.ControllerName.ToLower());   
     return controller; 
    } 
} 

그리고 글로벌 구성 파일에이 컨트롤러 팩토리를 등록했습니다. 동일한 프로세스가 다른 클래스에서는 작동하지만 apicontroller에서는 작동하지 않습니다.

아무에게도 제안 할 수 있습니까?

답변

0

웹 API 컨트롤러는 가로 채기가 불가능합니다. 그러나 필터를 사용하여 동일한 결과를 얻을 수 있습니다. http://damienbod.wordpress.com/2013/09/15/aop-with-asp-net-web-api/

당신은 여전히 ​​백엔드 코드의 로그 로깅 인터셉터를 사용할 수 있습니다 여기에

는 필터를 사용하여 컨트롤러에서 로깅을 할 수있는 방법을 보여주는 좋은 게시물입니다.