你怎么能注入web api IHttpRouteConstraint?

时间:2015-05-29 08:59:44

标签: c# .net asp.net-web-api dependency-injection simple-injector

正如标题所述,我需要能够将服务注入到web api约束类中。我在c#webApi项目中使用SimpleInjector。

在我的webApiConfig.cs中我有这个

// add constraint resolvers
var constraintResolver = new DefaultInlineConstraintResolver();
constraintResolver.ConstraintMap.Add("dynamicName", typeof(DynamicRouteConstraint));

// routing
config.MapHttpAttributeRoutes(constraintResolver);

我的自定义约束看起来像这样

public class DynamicRouteConstraint : IHttpRouteConstraint
{
    private IDynamicRouteService _service;

    public DynamicRouteConstraint(IDynamicRouteService service)
    {
        _service = service;
    }

    public bool Match(System.Net.Http.HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
    {
        return _service.Match(values[parameterName].ToString());
    }
}

如您所见,我需要将IDynamicRouteService注入我的约束中。目前我的应用程序正在抛出一个错误

  

没有为此对象定义无参数构造函数。

但我不想要无参数构造函数,因为我需要注入服务。

我已将此行添加到我的SimpleInjectorConfig文件中,其中我注册了所有要注入的类

container.RegisterWebApiRequest<IDynamicRouteService, DynamicRouteService>();

我已经创建了实现界面的实际服务,这里是(缩减​​版)

public class DynamicRouteService : IDynamicRouteService
{
    private IModuleService _service;

    public DynamicRouteService(IModuleService service)
    {
        _service = service;
    }

    public ICollection<DynamicRouteModel> GetRoutes()
    {
        var list = new List<DynamicRouteModel>();

        // custom code usually here

        return list;
    }

    public void Clear()
    {
        // custom code usually here
    }

    public bool Match(string name)
    {
        // custom code usually here
        return false;
    }

}

注入我的IModuleService服务没有任何问题,因为它工作正常,我只需要能够将IDynamicRouteService注入我的约束中。

任何帮助表示赞赏

修改

这是我当前的SimpleInjector Register方法

public static Container Register(HttpConfiguration apiConfig)
{
    var container = new Container();

    container.RegisterPerWebRequest<HttpContextBase>(() => new HttpContextWrapper(HttpContext.Current));
    container.RegisterPerWebRequest<HttpConfiguration>(() => apiConfig);

    // EnableHttpRequest and IDynamicNameService are needed to get the name of the module
    // that is requested via the DynamicDataController
    container.EnableHttpRequestMessageTracking(apiConfig);
    container.RegisterWebApiRequest<IDynamicModuleService, DynamicRouteService>();

    // service for dynamicRoute checking
    container.Register<IDynamicRouteService, DynamicRouteService>();

    // This is an extension method from the integration package.
    container.RegisterWebApiControllers(apiConfig);

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

    apiConfig.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

    return container;
}

我的startup.cs文件

public void Configuration(IAppBuilder app)
{
    // This is a self hosted webapi project..
    HttpConfiguration apiConfig = new HttpConfiguration();

    // confgi oauth
    OAuthConfig.Register(app);

    // simple injector
    var container = SimpleInjectorConfig.Register(apiConfig);

    AutoMapperConfig.RegisterMappings();
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(apiConfig, container);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    app.UseWebApi(apiConfig);
}

1 个答案:

答案 0 :(得分:3)

  

更新

所以我看了DefaultInlineConstraintResolver类的实现,发现这个类没有使用Container解析器,但是 Activator 类来创建可能导致的实例你是Default构造函数实例的问题。

IMO您可能需要为IInlineConstraintResolver创建自定义实现,或者从DynamicRouteConstraint构造函数中删除参数IDynamicRouteService并通过其他方式初始化实例。