Class level interception on

395 views
Skip to first unread message

Joel

unread,
Mar 16, 2011, 8:29:10 AM3/16/11
to ninject
I have the following line configured


kernel.Bind<IService>().To<Service>().Intercept().With<ValidationInterceptor>();

where ValidationInterceptor inherits off SimpleInterceptor.

What I would like to do is have an easy way to make all virtual
methods of a class be intercepted with maybe 1 to 3 interceptors
depending on what the class does. Perhaps I need to approach this
problem differently.

This fails because the Serivce needs to have a IRepository injected in
to it so it does not have a parameterless constructor. I get the
following message:

Can not instantiate proxy of class: Service.
Could not find a parameterless constructor.
Parameter name: constructorArguments

Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type
proxyType, List`1 proxyArguments, Type classToProxy, Object[]
constructorArguments) +412
Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, Object[] constructorArguments,
IInterceptor[] interceptors) +373
Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
classToProxy, ProxyGenerationOptions options, Object[]
constructorArguments, IInterceptor[] interceptors) +49

Ninject.Extensions.Interception.ProxyFactory.DynamicProxy2ProxyFactory.Wrap(IContext
context, InstanceReference reference) in C:\Source Code\SkilledTime\Web
\lib\Ninject.Extensions.Interception.2.2.0.0\src
\Ninject.Extensions.Interception.DynamicProxy2\DynamicProxy2ProxyFactory.cs:
103

Ninject.Extensions.Interception.Activation.Strategies.ProxyActivationStrategy.Activate(IContext
context, InstanceReference reference) in C:\Source Code\SkilledTime\Web
\lib\Ninject.Extensions.Interception.2.2.0.0\src
\Ninject.Extensions.Interception\Activation\Strategies
\ProxyActivationStrategy.cs:39

Ninject.Activation.<>c__DisplayClass2.<Activate>b__0(IActivationStrategy
s) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Pipeline.cs:
58

Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1
series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject
\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:23
Ninject.Activation.Pipeline.Activate(IContext context,
InstanceReference reference) in c:\Projects\Ninject\ninject\src\Ninject
\Activation\Pipeline.cs:58
Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject
\src\Ninject\Activation\Context.cs:182
Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects
\Ninject\ninject\src\Ninject\KernelBase.cs:375

LOBOMINATOR

unread,
Mar 16, 2011, 9:11:07 AM3/16/11
to nin...@googlegroups.com
Might be a dump question, but do you have the binding for IRepository
defined?

Daniel

--
You received this message because you are subscribed to the Google Groups
"ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to
ninject+u...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/ninject?hl=en.


Joel

unread,
Mar 16, 2011, 4:14:47 PM3/16/11
to ninject
Yep. Everything works until I add on
the .Intercept().With<ValidationInterceptor>(); then I start getting
the above error.

I can get Interception to work if I do

kernel.Bind<Service>.ToSelf().Intercept().With<ValidationInterceptor>()

and do have a parameterless constructor Service like in the examples.
I just cant get the interface binding and validation working together.

Remo Gloor

unread,
Mar 16, 2011, 8:46:49 PM3/16/11
to ninject
You need a parameterless ctor if I remember correctly. The reason for
this is that a class proxy derives from the proxied class and has to
call some base constructor. If no parameterless constructor exists we
would need to create one that has the same parameters and create some
parameters to inject into the proxy.

We planned to add support to create proxies based on Interfaces though
for the next release. With this change the need for a parameterless
constructor will be removed.

Joel

unread,
Mar 17, 2011, 3:34:13 AM3/17/11
to ninject
Thanks for your help. I didnt try having my constructor as well as an
default one, but having them both solves the problem I was having.

Sounds like a good feature for the next release.
Reply all
Reply to author
Forward
0 new messages