Request Scope

Topics: Web Api
Jun 1, 2011 at 11:42 PM

There are certain pieces of information that I would like my IoC container to scope to the current Request. One example of this would be the database session. In the past, I've configured my container to cache this in HttpContext.Current. What is the proper way to do this in the new Web API. I assume I'll be using a MessageHandler, but once I create the session in the handler, where should I stash it?

Jun 3, 2011 at 11:23 PM

Just bumping this again since it's pretty critical to almost any application.

Coordinator
Jun 3, 2011 at 11:33 PM
Edited Jun 4, 2011 at 6:49 AM
Kzu has a sample showing one way to do that with Autofac using instance context extensions.

Alternative approach is use HttpRequestMessage properties. It is a prop bag that you can stuff a request level container in. Then you can have a message handler that stuffs it in on the request, and pulls it out on the response to do cleanup.

Jun 3, 2011 at 11:35 PM
This approach is working beautifully for me with Autofac. You should be able to re-purpose for any other IoC I guess:

How to use Autofac with WCF Web API: http://kzu.to/j7Abjc
Coordinator
Jun 3, 2011 at 11:46 PM
Edited Jun 4, 2011 at 6:50 AM
Did you consider request props Daniel?
Jun 4, 2011 at 1:27 AM
didn't even know those existed.

isn't the extension hook I'm using appropriate for this?
Coordinator
Jun 4, 2011 at 2:41 AM
Edited Jun 4, 2011 at 6:50 AM
This is http specific. You can put whatever request level info you want in properties.

Jun 4, 2011 at 6:06 AM
IResourceFactory is also HTTP specific, after all, it's in Microsoft.ApplicationServer.Http ;)

And I believe the InstanceContext is also per-request, right?
Coordinator
Jun 4, 2011 at 6:43 AM

Well if the service is a singleton I belive the IC is the same for every request. HttpRequestMessage however will always change per request regardless.

Jun 4, 2011 at 6:48 AM
can you make the service a singleton using the webapi? (this might be a really dumb question, I'm far from a WCF expert)
Coordinator
Jun 4, 2011 at 6:49 AM

Yes if you annotate it with the ServiceBehavior attribute and set it to a singleton.

Glenn

Jun 4, 2011 at 1:51 PM
sold.

so the flow would be:
- message handler creates the per-request container, puts it in message properties (incoming)
- resource factory takes it out of the message and uses it to instantiate the resource
- message handler removes it from the message properties.

right?
Coordinator
Jun 4, 2011 at 3:05 PM
Yep

Sent from my Windows Phone

From: dcazzulino
Sent: Saturday, June 04, 2011 6:51 AM
To: Glenn Block
Subject: Re: Request Scope [wcf:259868]

From: dcazzulino

sold.

so the flow would be:
- message handler creates the per-request container, puts it in message properties (incoming)
- resource factory takes it out of the message and uses it to instantiate the resource
- message handler removes it from the message properties.

right?
Coordinator
Jun 4, 2011 at 3:34 PM
Yep

Sent from my Windows Phone

From: dcazzulino
Sent: Saturday, June 04, 2011 6:51 AM
To: Glenn Block
Subject: Re: Request Scope [wcf:259868]

From: dcazzulino

sold.

so the flow would be:
- message handler creates the per-request container, puts it in message properties (incoming)
- resource factory takes it out of the message and uses it to instantiate the resource
- message handler removes it from the message properties.

right?