Last active
August 29, 2015 14:24
-
-
Save lurumad/4114b2fb07adeb206f41 to your computer and use it in GitHub Desktop.
WebApi Unit of Work using DelegationHandler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class UnitOfWorkDelegatingHandler : DelegatingHandler | |
| { | |
| protected override async Task<HttpResponseMessage> SendAsync( | |
| HttpRequestMessage request, | |
| CancellationToken cancellationToken) | |
| { | |
| var response = await base.SendAsync(request, cancellationToken); | |
| var isSafeMethod = request.Method == HttpMethod.Get || request.Method == HttpMethod.Head; | |
| if (!response.IsSuccessStatusCode || isSafeMethod) | |
| { | |
| return response; | |
| } | |
| var dbContext = (EntitiesDbContext)request.GetDependencyScope().GetService(typeof(EntitiesDbContext)); | |
| await dbContext.SaveChangesAsync(cancellationToken); | |
| return response; | |
| } | |
| } |
Author
Muy buena recomendación! Lo he modificado para que funcione con los verbos considerados seguros según esta página http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Muchas gracias!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Viéndolo con Rodrigo, se nos ocurre si se podría optimizar para no tener que llamar al SaveChangesAsync si el request es de tipo GET por ejemplo