WebAPI preview 6 came with the full source and a bunch of samples - do we have something similar for Asp.Net WebAPI as well? More specifically, I'm looking for the equivalent of the TaskDispatcher sample - where the return type Task<T> of the method was
being converted to type T in the response.
Thanks,
Priya
Please "Mark as Answer" if this resolves your query. Thanks!
Thanks again! I was trying to figure out how to change the return type of a method. So, if my method (action) has a return type MyCustomResponse<T>, I want to actually return just T to the caller. In the behavior (previous version), this as achieved by a)
modifying the operation description and b) changing the actual return type via an operation invoker.
Please "Mark as Answer" if this resolves your query. Thanks!
where the return type Task<T> of the method was being converted to type T in the response.
ASP.NET Web API already handles the case where you return Task<T>. Basically any response type gets wrapped into a Task<HttpResponseMessage>, regardless of whether your action method returns T, Task<T>, HttpContent, Task<HttpContent>, HttpResponseMessage,
or Task<HttpResponseMessage>
If it is a custom response (e.g, Dto<T>) then there are a couple of ways to accomplish this. I would first look at using an ActionFilter to change the return type.
Thanks Dave...will try that and/or resultfilter - although I was thinking along the lines of altering response in a message handler. In the previous version, WebAPI used to throw an error if I only altered the response without changing the operation description.
Will need to see what it does now. I'm not an MVC person but I was thinking that if WebAPI complains, I may need to alter the action description via a custom descriptor etc. (no idea if that's even possible!).
Please "Mark as Answer" if this resolves your query. Thanks!
I've verified that I can change the response type at will - both from a delegating handler as well as the WebAPI specific ActionFilter. Any recommendations on which is the preferred way (since they can both do the job)?
Thanks,
Priya
Please "Mark as Answer" if this resolves your query. Thanks!
The Action filters seems to fit bettter in this scenario; you are modifying the action's result. Additionally, it is more discoverable for anyone reading your action code.
I view handlers as place for implementing concerns that are orthogonal to your action.
priya_marwah...
Member
81 Points
52 Posts
Source code for Web API & TaskDispatcher sample
Feb 29, 2012 06:38 PM|LINK
WebAPI preview 6 came with the full source and a bunch of samples - do we have something similar for Asp.Net WebAPI as well? More specifically, I'm looking for the equivalent of the TaskDispatcher sample - where the return type Task<T> of the method was being converted to type T in the response.
Thanks,
Priya
davebettin
Member
313 Points
94 Posts
Re: Source code for Web API & TaskDispatcher sample
Feb 29, 2012 06:50 PM|LINK
Hi Priya,
This source code is not yet available. Read the Faq for more details: http://wcf.codeplex.com/wikipage?title=WCF%20Web%20API%20is%20now%20ASP.NET%20Web%20API.
What exactly are you trying to accomplish with Task<T> in the new bits?
Cheers,
Dave
@dbettin
priya_marwah...
Member
81 Points
52 Posts
Re: Source code for Web API & TaskDispatcher sample
Feb 29, 2012 06:55 PM|LINK
Thanks again! I was trying to figure out how to change the return type of a method. So, if my method (action) has a return type MyCustomResponse<T>, I want to actually return just T to the caller. In the behavior (previous version), this as achieved by a) modifying the operation description and b) changing the actual return type via an operation invoker.
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Source code for Web API & TaskDispatcher sample
Feb 29, 2012 08:44 PM|LINK
ASP.NET Web API already handles the case where you return Task<T>. Basically any response type gets wrapped into a Task<HttpResponseMessage>, regardless of whether your action method returns T, Task<T>, HttpContent, Task<HttpContent>, HttpResponseMessage, or Task<HttpResponseMessage>
ASP.NET Team
@marcind
Blog
davebettin
Member
313 Points
94 Posts
Re: Source code for Web API & TaskDispatcher sample
Feb 29, 2012 09:01 PM|LINK
If it is a custom response (e.g, Dto<T>) then there are a couple of ways to accomplish this. I would first look at using an ActionFilter to change the return type.
Cheers,
Dave
@dbettin
priya_marwah...
Member
81 Points
52 Posts
Re: Source code for Web API & TaskDispatcher sample
Mar 01, 2012 04:03 AM|LINK
Thanks...but in my case, I want to wrap it in a custom response class, not Task.
priya_marwah...
Member
81 Points
52 Posts
Re: Source code for Web API & TaskDispatcher sample
Mar 01, 2012 04:24 AM|LINK
Thanks Dave...will try that and/or resultfilter - although I was thinking along the lines of altering response in a message handler. In the previous version, WebAPI used to throw an error if I only altered the response without changing the operation description. Will need to see what it does now. I'm not an MVC person but I was thinking that if WebAPI complains, I may need to alter the action description via a custom descriptor etc. (no idea if that's even possible!).
priya_marwah...
Member
81 Points
52 Posts
Re: Source code for Web API & TaskDispatcher sample
Mar 05, 2012 11:52 AM|LINK
I've verified that I can change the response type at will - both from a delegating handler as well as the WebAPI specific ActionFilter. Any recommendations on which is the preferred way (since they can both do the job)?
Thanks,
Priya
davebettin
Member
313 Points
94 Posts
Re: Source code for Web API & TaskDispatcher sample
Mar 05, 2012 02:45 PM|LINK
The Action filters seems to fit bettter in this scenario; you are modifying the action's result. Additionally, it is more discoverable for anyone reading your action code.
I view handlers as place for implementing concerns that are orthogonal to your action.
@dbettin
priya_marwah...
Member
81 Points
52 Posts
Re: Source code for Web API & TaskDispatcher sample
Mar 06, 2012 04:23 AM|LINK
Makes sense, thank you!