RequestFilters CacheClient

72 views
Skip to first unread message

quaffapint

unread,
Dec 8, 2011, 7:08:15 PM12/8/11
to servic...@googlegroups.com
In App_Start under the Configure function I added a this.RequestFilters.Add((req, res, dto) piece for authorization check.

I look up some data in the database and want to cache it to prevent constant lookups.  What I can't seem to figure out is how to use ICacheClient inside the App_Start file.  It always never inits and I get an error. It works fine in other classes outside of App_Start.  I'm calling it after the line...
container.Register<ICacheClient>(new MemoryCacheClient());

Where/How do I define public ICacheClient CacheClient { get; set; } in App_Start so I can cache my lookup in the RequestFilter?

Thanks very much.

Demis Bellot

unread,
Dec 8, 2011, 7:15:50 PM12/8/11
to servic...@googlegroups.com
Hi, 

Yeah the container is not passed in so you'll have to reference the container from elsewhere either via a closure:

container.Register<ICacheClient>(new MemoryCacheClient());
...
this.RequestFilters.Add((req, res, dto) => {
    var cacheClient = container.Resolve<ICacheClient>();
});

Or via the singleton

this.RequestFilters.Add((req, res, dto) => {
    var cacheClient = AppHostBase.Instance.Container.Resolve<ICacheClient>();
});

Not pretty, but it saves the extra argument in the Request Filters signature.
Although if there's interest? I may make the Container available off the request so you could do something like:

this.RequestFilters.Add((req, res, dto) => {
    var cacheClient = req.TryResolve<ICacheClient>();
});

Cheers,

Aaron Fischer

unread,
Jun 12, 2012, 6:05:28 PM6/12/12
to servic...@googlegroups.com
Could the request filter fill it's self from the container with anything implementing IRequestFilter?  This way the container could handle all dependencies.  Otherwise what is your recommendation for unit testing request filters?

Demis Bellot

unread,
Jun 12, 2012, 6:08:48 PM6/12/12
to servic...@googlegroups.com
Note: I've since added the Container to the IHttpRequest so it's available on all filters as well, so this now works:

this.RequestFilters.Add((req, res, dto) => {
    var cacheClient = req.TryResolve<ICacheClient>();
});


Cheers,


On Tue, Jun 12, 2012 at 6:05 PM, Aaron Fischer <pretze...@gmail.com> wrote:
Could the request filter fill it's self from the container with anything implementing IRequestFilter?  This way the container could handle all dependencies.  Otherwise what is your recommendation for unit testing request filters?



Reply all
Reply to author
Forward
0 new messages