web api self host custom ioc向控制器注入数据

时间:2015-10-02 20:38:03

标签: c# asp.net-web-api inversion-of-control ioc-container self-hosting

我的web api自托管应用程序中有一个属性,我想注入我的控制器,使用我的自定义IoC框架通过反射加载,这是我的启动代码:

public CustomClass StuffInstance { get; set; }


// This method is required by Katana:
public void Configuration(IAppBuilder app)
{
    ConfigureOAuth(app);
    var webApiConfiguration = ConfigureWebApi();

    // Use the extension method provided by the WebApi.Owin library:
    app.UseWebApi(webApiConfiguration);
}

我的控制器主要是脚手架,有些像:

// PUT: api/EventTypeDescriptions/5
[ResponseType(typeof(void))]
public IHttpActionResult PutStuff(int id, int something)
{
    //do stuff
    //here i would like to use StuffInstance like a singleton
    return StatusCode(HttpStatusCode.NoContent);
}  

如何将StuffInstance注入我的控制器?这些信息与任何制作IoC框架btw

的人有关

2 个答案:

答案 0 :(得分:0)

由于您提到AutoFac是潜在候选人,我建议您按照他们的tutorial进行WebAPI集成。您需要在CustomClass上定义一个接口,以便您可以正确地注入它。

您需要通过将其注册为实例组件来注入您已创建的实例(因为您希望将其视为单例)。

public interface ICustomClass {}
public class CustomClass : ICustomClass {}

public CustomClass _stuffInstance = new CustomClass();

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);

        var builder = new ContainerBuilder();
        var config = new HttpConfiguration();

        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        builder.RegisterInstance(_stuffInstance).As<ICustomClass>();

        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }
}

然后,在每个控制器的构造函数中,注入已绑定到相应接口的实例。

public class CustomController : ApiController
{
    private readonly ICustomClass _customClass;

    public CustomController(ICustomClass customClass)
    {
        _customClass = customClass;
    }    
}

答案 1 :(得分:0)

我在此链接中找到了将实例注入控制器的信息:

http://www.asp.net/web-api/overview/advanced/dependency-injection

基本上我为我的自定义IoC库实现了一个依赖项解析器

万一有人遇到同样的问题,这里是代码,也许对于其他IoC框架,它需要更多的工作

public class CustomIocDependencyResolver : IDependencyResolver
{
    private readonly CustomIoc container;

    public ComponentLoaderWebApiDependencyResolver(CustomIoc container)
    {
        this.container = container;
    }

    IDependencyScope IDependencyResolver.BeginScope()
    {
        return new CustomIocDependencyResolver(container);
    }

    Object IDependencyScope.GetService(Type serviceType)
    {
        return container.GetInstance(serviceType);
    }

    IEnumerable<Object> IDependencyScope.GetServices(Type serviceType)
    {
        return container.GetAllInstances(serviceType);
    }

    public void Dispose()
    {
    }
}

现在我的katana配置如下:

    // This method is required by Katana:
    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);
        var config = ConfigureWebApi();

        config.DependencyResolver = CustomIocDependencyResolver(container);

        // Use the extension method provided by the WebApi.Owin library:
        app.UseWebApi(config);
    }

将容器作为我的自定义IoC的实例