0

I am new to IOC.

I've MethodProfilerAspectAttribute attribute which has to be applied on any method like this

[MethodProfilerAspectAttribute(5)]
public void MethodName(){}

Here is the implementation of MethodProfilerAspectAttribute

[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class MethodProfilerAspectAttribute : OnMethodBoundaryAspect
{
        public ILogger logger { get; set; }
        public int x{get;set;}
        public MethodProfilerAspectAttribute(int x)
        {
            this.x=x;
        }
    public override void OnSuccess(MethodExecutionArgs args)
        {
            logger.CustomLogging("logMe");
            base.OnSuccess(args);
        }
}

I want to resolve my ILogger dependency using Log4NetLogger which is registered and resolving constructor dependencies properly by using following :

container.Register(Component.For<ILogger>().ImplementedBy(typeof(Log4NetLogger)));

but unfortunately whatever I've tried for resolving property dependency, is not working.

Any help would be greatly appreciated.

Ajay Suwalka
  • 549
  • 1
  • 7
  • 24
  • Possible duplicate of [Unity Property Injection on AuthorizeAttribute](http://stackoverflow.com/questions/29915192/unity-property-injection-on-authorizeattribute) – qujck May 06 '16 at 10:15
  • @qujck, No it's not. First, I am expecting it's resolution for Castle Windsor not Unity, Secondly watever method I've seen, are non working. I am expecting the code for Castle Windsor (C# ) only ,. – Ajay Suwalka May 06 '16 at 10:21
  • moreover that link doesn't solve my issue – Ajay Suwalka May 06 '16 at 10:22
  • 2
    I agree that the link doesn't solve your issue; it explains why you can't inject dependencies into attributes at the point they are constructed by the run-time. – qujck May 06 '16 at 10:35
  • No I can see on internet that It's possible, I agree that its a bad practice, But I don't have the other way out here. I need to resolve that for my attributes, Otherwise I can't go ahead – Ajay Suwalka May 06 '16 at 10:42
  • @AjaySuwalka can you link to an example showing it works? I very much doubt that it's possible... – Patrick Quirk May 06 '16 at 12:02
  • @PatrickQuirk https://github.com/castleproject/Windsor/blob/master/docs/how-properties-are-injected.md – Ajay Suwalka May 06 '16 at 12:30
  • the page you link to says *"Property injection of dependencies is designed to be done during component activation when a component is created."*. This does not hold true for attributes because attributes are created by the run-time and not the container. – qujck May 06 '16 at 12:54

1 Answers1

0

The link you provided just describes property injection for components resolved from the container. Attributes are not resolved from the container, but instead are created by the CLR. You might be able to jigger a way to set attribute properties by providing a custom IContributeComponentModelConstruction implementation, but I'm not so sure. See answers for similar questions here, here, and here (from the creator of Windsor).

In any case, attributes is not where you want to put functionality. They should be minimal, just providing metadata. I see here you're trying to provide some sort of functionality across all method invocations. You may want to consider Windsor's interceptors to provide similar behavior.

Community
  • 1
  • 1
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88