【问题标题】:Error while trying to use an IoC container inside WebApi (Net Framework)尝试在 WebApi (Net Framework) 中使用 IoC 容器时出错
【发布时间】:2018-08-18 08:54:40
【问题描述】:

我正在尝试在 WebApi 模板中使用 IoC 容器。我创建了一个空的 Web App Framework 项目,并选择使用 WebApi。然后我添加了 Unity NuGet 包并将以下内容添加到 Application_Start:

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    UnityConfig.Register();
}

UnityConfig:

public class UnityConfig
{
    public static UnityContainer Register()
    {
        var container = new UnityContainer();
        container.RegisterType<IMyClass, MyClass>();

        GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);

        return container;
    }

}

我的 UnityResolver 在这里(代码大部分是抄袭的,但看起来很标准):

public class UnityResolver : IDependencyResolver
{
    protected IUnityContainer container;

    public UnityResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        this.container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return new List<object>();
        }
    }

    public IDependencyScope BeginScope()
    {
        var child = container.CreateChildContainer();
        return new UnityResolver(child);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        container.Dispose();
    }
}

当我现在运行它时,我收到以下错误:

Unity.Exceptions.ResolutionFailedException: '解决 依赖失败,类型 = 'System.Web.Http.Hosting.IHostBufferPolicySelector',名称 = '(无)'。 异常发生时:解决时。例外是: InvalidOperationException - 当前类型, System.Web.Http.Hosting.IHostBufferPolicySelector,是一个接口和 无法构建。您是否缺少类型映射? - - - - - - - - - - - - - - - - - - - - - - - - 当时异常中,容器是:正在解决 System.Web.Http.Hosting.IHostBufferPolicySelector,(none) '

显然,在告诉它我要使用哪个 IoC 库之后,我需要注入一些内部依赖项,或者告诉它,或者可能更早(或更晚)注册它。

【问题讨论】:

  • 你在使用Unity.WebApi Nuget 包吗?
  • 不 - 只是 Unity。
  • 那是你的问题,你已经为 MVC 设置了 DI,而不是 WebAPI。

标签: c# asp.net-web-api web-applications inversion-of-control unity-container


【解决方案1】:

显然,在告诉它我要使用哪个 IoC 库之后,我需要注入一些内部依赖项,或者告诉它,或者可能更早(或更晚)注册它。

确实如此。您需要注册将通过 Unity 解析的所有类型。错误信息表明您缺少IHostBufferPolicySelector,因此您需要注册它。

public static UnityContainer Register()
{
    var container = new UnityContainer();

    // Register all types with Unity here
    container.RegisterType<IHostBufferPolicySelector, OwinBufferPolicySelector>();

    GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);

    return container;
}

我不确定OwinBufferPolicySelector 是否是您需要的,因为您没有提供足够的信息,但您确实需要为其映射一个实现(可能还有其他几个接口)才能继续。

如 cmets 中所述,如果您安装其中一个预制 Unity 包(例如 Unity.WebAPI)来提供此实现,可能会更容易。

如果您遵循 Dependency Injection in ASP.NET Web API 2 文档,也会有很大帮助。

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2021-01-26
    相关资源
    最近更新 更多