Last active
August 29, 2015 14:27
-
-
Save gyuwon/38c518b945fc65cd78e5 to your computer and use it in GitHub Desktop.
DependencyParameterBinding
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
using System; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Metadata; | |
namespace Envicase.Bindings | |
{ | |
public class DependencyBindingAttribute : ParameterBindingAttribute | |
{ | |
public override HttpParameterBinding GetBinding( | |
HttpParameterDescriptor parameter) | |
{ | |
return new DependencyParameterBinding(parameter); | |
} | |
} | |
public class DependencyParameterBinding : HttpParameterBinding | |
{ | |
public DependencyParameterBinding(HttpParameterDescriptor descriptor) | |
: base(descriptor) | |
{ | |
} | |
public override Task ExecuteBindingAsync( | |
ModelMetadataProvider metadataProvider, | |
HttpActionContext actionContext, | |
CancellationToken cancellationToken) | |
{ | |
var controllerContext = actionContext.ControllerContext; | |
var resolver = controllerContext.Configuration.DependencyResolver; | |
if (resolver != null) | |
{ | |
var scope = actionContext.Request.GetDependencyScope(); | |
var service = scope.GetService(Descriptor.ParameterType); | |
var arguments = actionContext.ActionArguments; | |
arguments[Descriptor.ParameterName] = service; | |
} | |
return Task.FromResult(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment