Skip to content

Instantly share code, notes, and snippets.

@gyuwon
Last active August 29, 2015 14:27
Show Gist options
  • Save gyuwon/38c518b945fc65cd78e5 to your computer and use it in GitHub Desktop.
Save gyuwon/38c518b945fc65cd78e5 to your computer and use it in GitHub Desktop.
DependencyParameterBinding
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