Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make GetAttribute in Interception.cs threadsafe

Prevent creation of proxies when there are no interceptors bound
  • Loading branch information...
commit d8af5faf0bd9c476dd6e11a0fc240cc791b2d804 1 parent 7aaf029
myoon authored
View
6 Snap.Autofac/AutofacAspectModule.cs
@@ -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;
}
View
16 Snap.CastleWindsor/CastleAspectFacility.cs
@@ -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)));
+ }
}
}
}
View
3  Snap.LinFu/AspectPostProcessor.cs
@@ -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;
}
View
9 Snap.Ninject/AspectProxyActivationStrategy.cs
@@ -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);
}
View
6 Snap.StructureMap/StructureMapAspectInterceptor.cs
@@ -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);
}
View
6 Snap.StructureMap/StructureMapDefinedAspectInterceptor.cs
@@ -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
56 Snap/Interception.cs
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.