7

Closed

Problems using InterfaceInterceptor on derived Interface

description

<p>Hi There!</p> <p>Right now I&#39;m having a problem with Unity InterfaceInterceptor because it will fail when constructing the dynamic type.</p> <p>My classes and interfaces are something like this:</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>public interface IService&lt;T&gt;</p> <p>{</p> <p> void Save(T entity);</p> <p>}</p> <p>&nbsp;</p> <p>public interface IOrderService : IService&lt;Order&gt;</p> <p>{</p> <p> void GetCustomerOrders(Customer customer);</p> <p>}</p> <p>&nbsp;</p> <p>public abstract class ServiceBase&lt;T&gt; : IService&lt;T&gt;</p> <p>{</p> <p> public void Save(T entity)</p> <p> {</p> <p> //Some code that saves a generic entity</p> <p> }</p> <p>}</p> <p>&nbsp;</p> <p>public class OrderService : IOrderService, ServiceBase&lt;Order&gt;</p> <p>{</p> <p> public void GetCustomerOrders(Customer customer)</p> <p> {</p> <p> //Some code that gets customer orders</p> <p> }</p> <p>}</p> <p>&nbsp;</p> <p>My Unity configuration code is as follows:</p> <p>&nbsp;</p> <p>IUnityContainer container = new UnityContainer();</p> <p>container.RegisterType&lt;IOrderService, OrderService&gt;();</p> <p>&nbsp;</p> <p>container.AddNewExtension&lt;Interception&gt;()</p> <p> .Configure&lt;Interception&gt;()</p> <p> .SetDefaultInterceptorFor&lt;IOrderService&gt;(new InterfaceInterceptor());</p> <p>&nbsp;</p> <p>var svc = container.Resolve&lt;IOrderService&gt;();</p> <p>&nbsp;</p> <p>It fails with a &quot;Method &#39;Save&#39; in type .... does not have an implementation&quot;.</p> <p>&nbsp;</p> <p>Any ideas how to tell Unity to get my type??</p> <p>&nbsp;</p> <p>Thanks in advance!</p>

file attachments

Closed Dec 11, 2009 at 9:53 PM by
Fixed in Unity 2.0

comments

jeremywiebe wrote Feb 3, 2009 at 1:58 PM

For what it's worth I ran into this problem today also. I traced it down to the "MethodsToIntercept" method in the InterfaceInterceptorClassGenerator class. The Type.GetMethods() call does not return methods from an inherited class. I've worked out something that fixes it for me (and I've written a few unit tests which I've also attached the new versions of the two files that I changed. I'm not sure this works for all cases, but it seems to work fine for both generic and non-generic base interfaces.

TimCools wrote May 14, 2009 at 10:44 PM

The proposed solutions does generate the proxy type, but does not intercept calls of the base interfaces. I posted a full working solution on my blog: http://www.timcools.net/post/2009/05/15/Unity-InterfaceInterceptor-doesnt-support-interface-inheritance.aspx

Hope this helps...

gmelnik wrote Aug 7, 2009 at 11:43 PM

Thanks for reporting this. Added to the Unity 2.0 backlog.

mvalenty wrote Nov 30, 2009 at 1:36 AM

This one bites me on a regular basis too. As a work around, you can extend MarshalByRefObject and use the TransparentProxyInterceptor instead.