I am trying to learn the Task and async features of the new .net 4.5, by wanting to know what is the best way for me to go.
I've got a few webapi controllers like the one below.
The webapi controller receives data and updates the sql databasse by useing the entity framework, not linqtosql.
How would I structure the code below to work in async?
Alos the [Authorize] attribute that I've got on the Put verb of the controller is that the same as MemberShip.GetUser() ?. In other words by using the authorize attribute it does in fact chect to see if the requesting client is authinacted and if the client
is not authinacted then the put request is denied?
Thanks!
public class SizeController : ApiController
{
[Authorize]
public void Put(Size s)
{
try
{
using (var db = new StorefrontSystemEntities())
{
db.proc_UpdateSize(
s.HeightInches, s.HeightPercent, s.WidthInches,
s.WidthPercent, s.ID,
s.Daylite.Width,
s.Daylite.Height
);
};
using (var db = new StorefrontSystemEntities())
{
if (s.Floor > 0)
{
db.proc_UpdateHorizontalFloor(s.Floor, s.ID);
};
};
using (var db = new StorefrontSystemEntities())
{
if (s.IsBay)
{
db.proc_UpdateBayIsFixed(s.ID, s.IsFixed);
};
};
}
catch (SqlException sqlEx)
{
throw new HttpResponseException(
Request.CreateResponse(HttpStatusCode.NotAcceptable, string.Format("Sql Number: {0}", sqlEx.Number)));
}
}
}
erikkl2000
Member
257 Points
234 Posts
Async as Task with web api
Feb 15, 2013 06:53 PM|LINK
I am trying to learn the Task and async features of the new .net 4.5, by wanting to know what is the best way for me to go.
I've got a few webapi controllers like the one below.
The webapi controller receives data and updates the sql databasse by useing the entity framework, not linqtosql.
How would I structure the code below to work in async?
Alos the [Authorize] attribute that I've got on the Put verb of the controller is that the same as MemberShip.GetUser() ?. In other words by using the authorize attribute it does in fact chect to see if the requesting client is authinacted and if the client is not authinacted then the put request is denied?
Thanks!
public class SizeController : ApiController { [Authorize] public void Put(Size s) { try { using (var db = new StorefrontSystemEntities()) { db.proc_UpdateSize( s.HeightInches, s.HeightPercent, s.WidthInches, s.WidthPercent, s.ID, s.Daylite.Width, s.Daylite.Height ); }; using (var db = new StorefrontSystemEntities()) { if (s.Floor > 0) { db.proc_UpdateHorizontalFloor(s.Floor, s.ID); }; }; using (var db = new StorefrontSystemEntities()) { if (s.IsBay) { db.proc_UpdateBayIsFixed(s.ID, s.IsFixed); }; }; } catch (SqlException sqlEx) { throw new HttpResponseException( Request.CreateResponse(HttpStatusCode.NotAcceptable, string.Format("Sql Number: {0}", sqlEx.Number))); } } }BrockAllen
All-Star
27520 Points
4900 Posts
MVP
Re: Async as Task with web api
Feb 15, 2013 06:55 PM|LINK
[Authorize] simply checks Thread.CurrentPrincipal.Identity.IsAuthenticated and if not returns a 401.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
panesofglass
Member
730 Points
237 Posts
Re: Async as Task with web api
Feb 26, 2013 01:59 PM|LINK
You aren't doing any async work. If you want to return a Task anyway, use: