Can't seem to get virtual method interception working

May 4, 2009 at 11:57 PM
It doesn't seem to pick up anything.
Here are my call handler classes

    public class CachingCallHandlerAttribute : HandlerAttribute
    {
        private TimeSpan _duration;

        public CachingCallHandlerAttribute()
        {
            _duration = new TimeSpan(0);
        }

        public CachingCallHandlerAttribute(TimeSpan duration)
        {
            _duration = duration;
        }

        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new CachingCallHandler(_duration);
        }
    }

    public class CachingCallHandler : ICallHandler
    {
        private TimeSpan _duration;
        private static IDCacheProvider _cache = ContainerFactory.Instance.GetContainer("CacheProviders").Resolve<IDCacheProvider>();

        public CachingCallHandler(TimeSpan duration)
        {
            duration = _duration;
        }

        #region ICallHandler Members
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            IMethodReturn msg;
            string cacheKey = input.MethodBase.Name;

            foreach (var arg in input.Arguments)
            {
                cacheKey += "_" + arg.ToString();
            }

            object data = _cache.Get(cacheKey);

            if (data == null)
            {
                msg = getNext()(input, getNext);

                if (_duration.Ticks == 0)
                {
                    _cache.Put(cacheKey, msg.ReturnValue);
                }
                else
                {
                    _cache.Put(cacheKey, msg.ReturnValue, _duration);
                }
            }
            else
            {
                msg = input.CreateMethodReturn(data);
            }

            return msg;
        }

        public int Order { get; set; }

        #endregion
    }

Here's how I use it:
public class DataProvider
        [CachingCallHandler()]
        public virtual IEnumerable<Campaign> GetCampaigns(string clientId)
{
// do stuff
}
...
}

And the class is instantiated like so, where ContainerFactory.Instance.GetContainer() is doing the work of extracting and instantiating the proper IUnityContainer:
private static DataProvider _provider = ContainerFactory.Instance.GetContainer("CacheInterceptor").Resolve<DataProvider>();

And finally a portion of my config file:

    <!-- Cache interceptor container -->
    <container name="CacheInterceptor">
      <extensions>
        <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
      </extensions>
      <extensionConfig>
        <add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">
          <interceptors>
            <interceptor type="Microsoft.Practices.Unity.InterceptionExtension.VirtualMethodInterceptor, Microsoft.Practices.Unity.Interception">
              <key type="DataProvider"/>
            </interceptor>
          </interceptors>
        </add>
      </extensionConfig>
    </container>



I set break points in the CachingCallHandlerAttribute and CachingCallHandler classes but it never makes it there.  It's almost like the attribute is ignored
May 5, 2009 at 12:36 AM
got the solution...my DataProvider class was not public, it was internal..and that made the difference