Permalink
Browse files

Make GetAttribute in Interception.cs threadsafe

Prevent creation of proxies when there are no interceptors bound
  • Loading branch information...
1 parent 7aaf029 commit d8af5faf0bd9c476dd6e11a0fc240cc791b2d804 myoon committed Feb 7, 2012
@@ -75,9 +75,9 @@ private void RegistrationActivating(object sender, ActivatingEventArgs<object> e
return;
}
- var proxy = (MasterProxy)e.Context.Resolve(typeof(MasterProxy));
-
- if (!e.Instance.IsDecorated(proxy.Configuration))
+ var proxy = (MasterProxy)e.Context.Resolve(typeof(MasterProxy));
+
+ if (!e.Instance.IsDecorated(proxy.Configuration) || proxy.Configuration.Interceptors.Count <= 0)
{
return;
}
@@ -55,12 +55,16 @@ private void KernelComponentRegistered(string key, IHandler handler)
}
//var proxy = (MasterProxy)Kernel[typeof (MasterProxy)];
- var proxy = Kernel.Resolve<MasterProxy>();
- handler.ComponentModel.Interceptors.AddIfNotInCollection(new InterceptorReference(typeof(MasterProxy)));
-
- for (var i = 1; i < proxy.Configuration.Interceptors.Count; i++)
- {
- handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(PseudoInterceptor)));
+ var proxy = Kernel.Resolve<MasterProxy>();
+
+ if (proxy.Configuration.Interceptors.Count > 0)
+ {
+ handler.ComponentModel.Interceptors.AddIfNotInCollection(new InterceptorReference(typeof(MasterProxy)));
+
+ for (var i = 1; i < proxy.Configuration.Interceptors.Count; i++)
+ {
+ handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(PseudoInterceptor)));
+ }
}
}
}
@@ -57,7 +57,8 @@ public void PostProcess(IServiceRequestResult result)
var proxy = result.Container.GetService<IMasterProxy>();
- if (!instance.IsDecorated(proxy.Configuration))
+ //Don't bother proxying anything if there are no interceptors
+ if (!instance.IsDecorated(proxy.Configuration) || proxy.Configuration.Interceptors.Count <= 0)
{
return;
}
@@ -45,10 +45,11 @@ public override void Activate(IContext context, InstanceReference reference)
// Don't try to IInterceptor or MasterProxy instances.
if (reference.Instance as IInterceptor == null && reference.Instance.GetType() != typeof(MasterProxy)) // as INinjectAspectConfiguration == null)
{
- var proxy = context.Kernel.Get<IMasterProxy>();
-
- // Only build a proxy for decorated types
- if (reference.Instance.IsDecorated(proxy.Configuration))
+ var proxy = context.Kernel.Get<IMasterProxy>();
+
+ // Only build a proxy for decorated types
+ // Don't bother proxying anything if there are no interceptors
+ if (proxy.Configuration.Interceptors.Count > 0 && reference.Instance.IsDecorated(proxy.Configuration))
{
reference.Instance = _proxyFactory.CreateProxy(reference.Instance, proxy);
}
@@ -53,9 +53,9 @@ public class StructureMapAspectInterceptor: TypeInterceptor
/// <returns></returns>
public object Process(object target, IContext context)
{
- var proxy = (MasterProxy)Container.GetInstance<IMasterProxy>();
-
- if(target.IsDecorated(proxy.Configuration))
+ var proxy = (MasterProxy)Container.GetInstance<IMasterProxy>();
+
+ if(proxy.Configuration.Interceptors.Count > 0 && target.IsDecorated(proxy.Configuration))
{
return _proxyFactory.CreateProxy(target, proxy);
}
@@ -84,11 +84,11 @@ private IMasterProxy GetMasterProxy()
/// <returns></returns>
public object Process(object target, IContext context)
{
- var proxy = GetMasterProxy();
-
+ var proxy = GetMasterProxy();
+
var name = target.GetType().FullName;
- if (target.IsDecorated(proxy.Configuration) && !name.EndsWith("Proxy")) //Don't create proxies of proxies
+ if (proxy.Configuration.Interceptors.Count > 0 && target.IsDecorated(proxy.Configuration) && !name.EndsWith("Proxy")) //Don't create proxies of proxies
{
return _proxyFactory.CreateProxy(target, proxy);
}
View
@@ -144,42 +144,46 @@ private static MethodBase GetTargetMethod(IInvocation invocation)
private static Attribute GetAttribute(Type targetType, MethodBase method, Type attributeType)
{
var key = GetMethodSignature(method, attributeType);
- if (SignatureCache.ContainsKey(key))
+
+ lock (SignatureCache) //Has to be threadsafe
{
- return SignatureCache[key];
- }
+ if (SignatureCache.ContainsKey(key))
+ {
+ return SignatureCache[key];
+ }
- var classAttributes = (from attr in targetType.GetCustomAttributes(!targetType.IsInterface)
- where attr.GetType().Equals(attributeType)
- select attr).ToList();
+ var classAttributes = (from attr in targetType.GetCustomAttributes(!targetType.IsInterface)
+ where attr.GetType().Equals(attributeType)
+ select attr).ToList();
- if (classAttributes.Any())
- {
- var attribute = (ClassInterceptAttribute)classAttributes.First();
+ if (classAttributes.Any())
+ {
+ var attribute = (ClassInterceptAttribute)classAttributes.First();
+
+ if (MatchesClassAttribute(attribute, method))
+ {
+ SignatureCache.Add(key, attribute);
+ return attribute;
+ }
+ }
- if (MatchesClassAttribute(attribute, method))
+ var attributes = (from attr in method.GetCustomAttributes(!targetType.IsInterface)
+ where attr.GetType().Equals(attributeType)
+ select attr).ToList();
+
+ if (attributes.Any())
{
+ var attribute = (Attribute)attributes.First();
SignatureCache.Add(key, attribute);
return attribute;
}
- }
-
- var attributes = (from attr in method.GetCustomAttributes(!targetType.IsInterface)
- where attr.GetType().Equals(attributeType)
- select attr).ToList();
+ else
+ {
+ SignatureCache.Add(key, null);
+ }
- if (attributes.Any())
- {
- var attribute = (Attribute)attributes.First();
- SignatureCache.Add(key, attribute);
- return attribute;
- }
- else
- {
- SignatureCache.Add(key, null);
+ return null;
}
-
- return null;
}
private static bool MatchesClassAttribute(ClassInterceptAttribute attribute, MethodBase method)

0 comments on commit d8af5fa

Please sign in to comment.