Problems using InterfaceInterceptor on derived Interface

Jan 22, 2009 at 11:02 PM
Hi There!
Right now I'm having a problem with Unity InterfaceInterceptor because it will fail when constructing the dynamic type.
My classes and interfaces are something like this:


public interface IService<T>
{
     void Save(T entity);
}

public interface IOrderService : IService<Order>
{
    void GetCustomerOrders(Customer customer);
}

public abstract class ServiceBase<T> : IService<T>
{
    public void Save(T entity)
    {
          //Some code that saves a generic entity
    }
}

public class OrderService : IOrderService, ServiceBase<Order>
{
   public void GetCustomerOrders(Customer customer)
   {
        //Some code that gets customer orders
   }
}

My Unity configuration code is as follows:

IUnityContainer container = new UnityContainer();
container.RegisterType<IOrderService, OrderService>();

container.AddNewExtension<Interception>()
                .Configure<Interception>()
                .SetDefaultInterceptorFor<IOrderService>(new InterfaceInterceptor());

var svc = container.Resolve<IOrderService>();

It fails with a "Method 'Save' in type .... does not have an implementation".

Any ideas how to tell Unity to get my type??

Thanks in advance!


Jan 27, 2009 at 8:21 AM
Taken from the documentation:  "InterfaceInterceptor - It can proxy only one interface on the object. "

I'm not sure if this statement also meant that that interface should not also inherit from another interface.  I need to verify this first.  Anyway, that's what the code currently does.  It only creates a proxy class implementing the interface you configured an interception for.  It does not look up the hierarchy of inheritance.  In your case, it was only able to see the GetCustomerOrders method of the IOrderService but not the Save method of the IService<T> interface.  If you don't want to change the structure of your classes, you can use the TransparentProxyInterceptor in place of the InterfaceInterceptor.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Jan 27, 2009 at 3:27 PM
Thanks for your help Sarah.

I am trying to use either VirtualMethodInterceptor or InterfaceInterceptor because TranspartentProxyInterceptor is the slowest of all. I've made some tests and I've discovered that VirtualMethodInterceptor is the fastest. The thing here is that in the code I put above, I cannot use the VirtualMethodInterceptor because if I configure de Interception for OrderService (obviously making all the methods virtual) the handlers won't get called.
I tried to RegisterType<OrderService> and that won't work either, I guess its because I'm asking Unity to Resolve <IOrderService>. The only way I've managed to make it work is asking Unity to resolve OrderService and that is a big NO because I need Unity to DI not just interception.
Is there any way to use VirtualMethodInterceptor? Will the future versions support InterfaceInterceptor to search it's inheritance?

Thanks!
Jan 27, 2009 at 7:12 PM
This sounds like a bug. I'll enter a work item so we don't lose it, and get back to you once I've looked at the code.

Jan 27, 2009 at 7:12 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 7, 2009 at 2:13 PM
When I try to go to the related work item I recieve following error:

The server experienced an error.

If you see this page, it is because an error occurred in the system while trying to process your request. We apologize for the inconvenience. This error has been reported to our team for analysis.

Error Log Reference #134d70e2-bdf4-4621-a287-b95775566659

Anybody experiencing the same problem?

Tim