5

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

在我webApiConfig.cs我有这个

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

// routing 
config.MapHttpAttributeRoutes(constraintResolver); 

我定制contraint看起来像这样

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注册方法

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); 
} 
+0

您正在使用[webAPI-SimpleInjector集成](https://www.nuget.org/packages/SimpleInjector.Integration.WebApi)包是否正确? – vendettamit

+0

是的,我相信我是 – Gillardo

+0

检查我的答案,如果我猜对了。所以你使用的是SimpleInjector,但是还有另一个关联库,它与SimpleInjector的WebAPI集成包一起提供,它实际上通过提供WebApi兼容容器来满足需求。 – vendettamit

回答

3

更新:

所以我看着DefaultInlineConstraintResolver类的实现,发现此类不使用集装箱解析器,但使用激活类来创建实例,它可能会引起你的默认构造方法的问题实例。

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

+0

谢谢!更新了答案。 – vendettamit