0

Any advice would be greatly appreciated on this.

I have a standard NTier application which implements Unity DI. This works absolutely fine, the unity container is created and registers everything within the BAL. I've also got interface interception configured using attribute decoration above methods. I'm trying to measure timings for certain methods. The interface interception works absolutely fine in the BAL layer but when I try to use the same method on say a layer called DistributionProviders which is referenced by the BAL it never hits the call handler.

The code I'm using to configure the interface interception is:

   _container.AddNewExtension<Interception>()
                .Configure<Interception>()
                .SetInterceptorFor<ISftpState>(new InterfaceInterceptor());

The Attribute is class is:

    public class ProcessLoggingAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new ProcessLoggingHander(); 
        }
    }

The ProcessLoggingHandler is:

public class ProcessLoggingHander : ICallHandler
{

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {

        Console.WriteLine("Starting");
       var methodReturn = getNext().Invoke(input, getNext); 

       Console.WriteLine("finishing");
       return methodReturn;            
    }

    public int Order { get; set; }
}

Example interface method decoration:

public interface ISftpState
{
     [ProcessLogging]
     bool Connect();
}

Can anyone explain to me what I'm doing wrong. The only thing I can think of is that the container is not being passed into the CreateHandler() method of the HandlerAttribute.

--Additional Code - This is how I'm registering the Unity Container. Its actually a property within a singleton. I just resolve the top level control class which then automatically resolves the other classes beneath it.

UnityContainer _container = new UnityContainer();
_container.RegisterType<ISftpState,SftpState>();
zeencat
  • 589
  • 1
  • 5
  • 26
  • When you say NTier do you mean that each tier is running in a different process? If so the container will need to be configured in each one. – Juan Mar 06 '15 at 16:45
  • By NTier I mean, BAL, and DAL as well as some other projects which sit in parallel. I'm using constructor injection within each of the layers as well. This seems to work fine which is why I'm a little at a lose as to why the interface interception isn't working in any layer other than the BAL. – zeencat Mar 06 '15 at 16:46
  • The BAL has a reference to a project called DistributionProviders. The BAL layer registers the the ISftpState interface to the unity container, the ISftpState interface belongs to the DistributionProviders project. The DI works absolutely fine, its just the interface intersception that doesnt work on the ISftpState. However if I register an interface that belongs to the BAL layer to use interface intersception it works fine. – zeencat Mar 06 '15 at 16:57
  • I've added a little more code. Not sure what else to show. There is only one container which registers everything for all the layers. – zeencat Mar 06 '15 at 17:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72425/discussion-between-zeencat-and-juan). – zeencat Mar 06 '15 at 17:08
  • How are you registering everything within the BAL? Are you registering by convention? – Ant Radha Mar 13 '15 at 10:39

1 Answers1

0

I'm not sure I fully understand your use case.

However, did you try putting your method attribute in the class that implements your interface? It seems like you only declared the attribute in the interface.

See this stackoverflow: Can a C# class inherit attributes from its interface?

Community
  • 1
  • 1
TchiYuan
  • 4,258
  • 5
  • 28
  • 35
  • Its interface interception. Applying the attribute to the concrete wouldn't solve the problem. The problem isn't that it doesn't work. Its that it doesn't work in any other projects except the BAL where the container is registered. – zeencat Mar 07 '15 at 09:12