5

StructureMap을 통해 MVC ActionFilter에 삽입 할 수없는 이유는 무엇입니까?Structmap을 Asp.Net MVC ActionFilter에 사용하여 SetterProperty 주입

public class LockProjectFilter : ActionFilterAttribute 
    { 
     [SetterProperty] 
     public ISecurityService SecurityService { get; set; } 

     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      var loggedinStaffId = SecurityService.GetLoggedInStaffId(); 
      if (loggedinStaffId == 1) 
       throw new ArgumentNullException(); 
      base.OnActionExecuting(filterContext); 
     } 
    } 


    public static IContainer Initialize() 
    { 
     ObjectFactory.Initialize(x => 
         { 
          x.Scan(scan => 
              { 
               scan.TheCallingAssembly(); 
               scan.WithDefaultConventions(); 
               scan.AssemblyContainingType<ISecurityService>(); 
              }); 
          x.SetAllProperties(p => p.OfType<ISecurityService>()); 
          //x.ForConcreteType<LockProjectFilter>().Configure 
           // .Setter(c => c.SecurityService).IsTheDefault(); 
         }); 
     return ObjectFactory.Container; 
    } 
+0

귀하의 질문에 내 문제를 내가 도와 친애하는 내가 [SetterProperty] 내 코드에서 그리워하고 당신이 완전한 세부 사항이 내 코드는 다음 링크를 참조 할 경우 작동 http://stackoverflow.com/questions/23386344/asp-net-mvc-5-custom-action-filter-with-structuremap – Developerzzz

답변

9

ObjectFactory에서 'BuildUp'메소드를 사용해야합니다.

[Test] 
    public void create_a_setter_rule_and_see_it_applied_in_BuildUp_through_ObjectFactory() 
    { 
     var theGateway = new DefaultGateway(); 
     ObjectFactory.Initialize(x => 
     { 
      x.ForRequestedType<IGateway>().TheDefault.IsThis(theGateway); 

      // First we create a new Setter Injection Policy that 
      // forces StructureMap to inject all public properties 
      // where the PropertyType is IGateway 
      x.SetAllProperties(y => 
      { 
       y.OfType<IGateway>(); 
      }); 
     }); 

     // Create an instance of BuildUpTarget1 
     var target = new BuildUpTarget1(); 

     // Now, call BuildUp() on target, and 
     // we should see the Gateway property assigned 
     ObjectFactory.BuildUp(target); 

     target.Gateway.ShouldBeTheSameAs(theGateway); 
    } 

http://docs.structuremap.net/ConstructorAndSetterInjection.htm#section4

그런 다음 당신은이 같은 새로운 FilterAttributeFilterProvider을 만들 수 있습니다
public class DependencyResolverFilterProvider : FilterAttributeFilterProvider 
{ 
    public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) 
    { 
     var filters = base.GetFilters(controllerContext, actionDescriptor); 

     foreach (var filter in filters) 
     { 
      //DI via Setter Injection 
      DependencyResolver.BuildUp(filter.Instance); 
     } 

     return filters; 
    } 
} 

그런 다음 마지막으로 .NET 파이프 라인에 사용자 정의 필터 공급자를 추가.

private static void RegisterProviderAndFilters() 
    { 
     var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider); 
     FilterProviders.Providers.Remove(oldProvider); 
     FilterProviders.Providers.Add(new DependencyResolverFilterProvider()); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
    } 

희망이 있습니다.

WM

+0

주입 된 속성이 공개이면이 기능은 매력처럼 작동합니다. 속성이 설정 됨). –