How to pass data between Message/Operation and ServiceContract

Topics: Web Api
Aug 2, 2011 at 9:35 AM
Edited Aug 2, 2011 at 6:03 PM

I wonder if there's some best practise to pass value between Message/Operation to ServiceContract.
I added a request interceptor for a specific servicecontract

var oAuthConfiguration = HttpHostConfiguration.Create()
.SetResourceFactory(new WindsorResourceFactory(container.Kernel))
.AddMessageHandlers(typeof(OAuthService));

RouteTable.Routes.MapServiceRoute<UserResource>("users", oAuthConfiguration);

In case "OAuthService" execution has been completed successfully and pipeline hit "UserResource", I'd like to trasfer some data between Message(s)/Operation(s) (OAuthService) and ServiceContract(UserResource).

Is there any bag/collection to trasfer those data? Somenthing like HttpContext.Items in asp.net pipeline...

I figure out as possible solution to write some data into request instance, but it doesn't look very nice/flexible...

        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            request.Content.Headers.Add("myData1", "2");

and than having same request object as input into ServiceContract Method

        [WebGet(UriTemplate = "{userName}")]
        public HttpResponseMessage<User> Get(string userName, HttpRequestMessage request)

Coordinator
Aug 4, 2011 at 8:35 AM

Yes, HttpRequestMessage has a properties collection. If you want to pack data into the request that the operation / operation handlers can pull out, put it in the properties.

Glenn

Jun 15, 2012 at 10:12 PM

Glenn,

Still on Preview 6, and wondering a clean way to pull out values from request.Properties w/o having to write the same code over and over again on each resource/operation. Is there any place/hook that allow me to do this?

Imagine I'm validating a API key/token in a MessageHandler and upon success I want to send some information that ALL resources/endpoints requiring authorization would use for audit purposes.  If I can find a common place where to do this, and set HttpContext.Current.Items with those properties and wrap that in a static class, then it would be easy to use that static class on every endpoint aforementioned. 

Is this possible?

Thanks,

Raciel