I'm trying to set up interception to work with Ninject which we have been using as our dependency injection framework for a while.
I have downloaded the interception extension from NuGet and tried it with both the Castle Dynamicproxy implementation and the LinFu implementation but could not be either to work with our applications.
Castle gave an error when creating a proxy on a class that did not have a parameterless constructor, since all the service objects have their dependencies injected via the constructor this is a problem. The error is:
System.ArgumentException: Can not instantiate proxy of class: emedia.RapidSystems.Subscriber.Presenters.RRSubmissionPresenter. Could not find a parameterless constructor. Parameter name: constructorArguments
The LinFu interceptor worked better, right up until the code called a method with a generic parameter then it gave me the following:
System.ArgumentException: Generic types are not valid. Parameter name: methodInfo
Here is a simplified version code for one of the classes I am trying to intercept:
[LogCalls]
public class Repository<T> : IRepository<T>
where T : class
{
public virtual T GetEntity<TKey>(ObjectContext context, TKey key)
{
var entity = GetEntity(context, key, _emptyLoadingStrategy);
return entity;
}
public virtual IQueryable<T> GetAll(ObjectContext context)
{
var query = GetAll(context, _emptyLoadingStrategy);
return query;
}
public virtual T Add(ObjectContext context, T entity)
{
context.AddObject(EntitySetName(context), entity);
return entity;
}
//other code goes here
}
Add and GetAll work fine but the error happens when GetEntity is called on the proxy.
At this point I'm stuck, as neither interceptor works with the code base. Has anyone got Ninject interception working with a real complex production system, rather than a simple demo class, and if so how? I don't mind which interceptor I use as long as it works.
Or is interception with Ninject just not mature enough yet, and do I need to look at replacing the whole thing with something else like Unity?
Use version 3.0.0-rc2. it adds support for interface proxies to dynamic proxy