I have implemented a custom OutputCacheProvider
public class MongoDBCacheProvider : OutputCacheProvider, IDisposable { ... }
The cacheprovider is registered like so:
<outputCache defaultProvider="MongoDBCacheProvider" enableOutputCache="true" >
<providers>
<add name="MongoDBCacheProvider" type="Mynamespace.Core.Caching.MongoDBCacheProvider, Mynamespace.Core" />
</providers>
</outputCache>
I need to pass some arguments to the constructor. I want to use Ninject to bind my cache provider.
this.Bind<System.Web.Caching.OutputCacheProvider>()
.To<Core.Caching.MongoDBCacheProvider>()
.WithConstructorArgument("databaseName", dbName);
More arguments have to be passed, but this is just an example. I’m sure simple solutions exist to somehow get that string there, but i would prefer to use Ninject like i use for all other classes.
This fails with the message: “No parameterless constructor defined for this object.” The following binding doesn’t work either.
this.Bind<Core.Caching.MongoDBCacheProvider>().ToSelf()
.InSingletonScope()
.WithConstructorArgument("databaseName", dbName);
I have verified that the binding runs before the error occurs. ASP .NET somehow bypasses the ninject bindings.
There doesn’t seem to be any way to plug in a factory for it either.
Does anybody know how i can pass constructor arguments to a derived OutputCacheProvider?
Thank you.
You should be able to
Injectinto the object early in your web app life cycle by causing it to be injected during app startup.NB you will have to use Property Setter Injection in preference to Constructor Injection, as there is likely no way to get the Cache Provider Provider to give you control of the instance creation.
See this blog post by @Remo Gloor