C# 配置Unity以使用Web api

C# 配置Unity以使用Web api,c#,unity-container,C#,Unity Container,Unity config新手,我正试图在我的项目中实现这一点。然而,我被卡住了 我收到以下错误:当前类型System.Web.Mvc.IControllerFactory是一个接口,无法构造。是否缺少类型映射 集装箱类 public class ContainerBootstrapper { public static void ConfigureUnityContainer() { //simple registeration

Unity config新手,我正试图在我的项目中实现这一点。然而,我被卡住了

我收到以下错误:当前类型System.Web.Mvc.IControllerFactory是一个接口,无法构造。是否缺少类型映射

集装箱类

public class ContainerBootstrapper
    {
        public static void ConfigureUnityContainer()
        {
            //simple registeration
            container.RegisterType<IProduct, ProductHelper>(); //maps interface to a concrete type

            System.Web.Mvc.DependencyResolver.SetResolver(new MyProjectControllerDependency(container));

        }

    }
接口

public interface IProduct
{
    List<ProductViewModel> GetProductByBarcode(string value);
    string GetProductPrice(string value);
}
公共接口IPProduct
{
列表GetProductByBarcode(字符串值);
字符串GetProductPrice(字符串值);
}
助手

public class MyProjectControllerDependency : IDependencyResolver
{
private IUnityContainer _container;

public MyProjectControllerDependency(IUnityContainer container)
{
    this._container = container;
}

public object GetService(Type serviceType)
{
    return _container.Resolve(serviceType);
}

public IEnumerable<object> GetServices(Type serviceType)
{
    return _container.ResolveAll(serviceType);
}
public class ProductHelper : IProduct
    {
        //private readonly IProduct _iproduct;

        //public ProductHelper(IProduct iproduct)
        //{
        //    this._iproduct = iproduct;
        //}

        public List<ProductViewModel> GetProductByBarcode(string value)
        {
            throw new NotImplementedException();
        }

        public string GetProductPrice(string value)
        {
            throw new NotImplementedException();
        }
}
public class ProductHelper:IProduct
{
//私有只读IPProduct _IPProduct;
//公共产品助手(IPProduct IPProduct)
//{
//这个。_ipproduct=ipproduct;
//}
公共列表GetProductByBarcode(字符串值)
{
抛出新的NotImplementedException();
}
公共字符串GetProductPrice(字符串值)
{
抛出新的NotImplementedException();
}
}

我不明白,我错过了什么?谁能给我指一下正确的方向吗

对于ASP.NET MVC和WebApi项目,仅使用Unity是不够的。对于这些项目,您还需要从nuget安装
Unity.MVC

这是一个现成的可用类。您只需要在UnityContainer中注册依赖项并完成

一旦安装了
Unity.MVC
,它就会创建类
unitymvcavator
UnityConfig
UnityConfig
类具有初始化UnityContainer的实现。您所需要做的就是在
RegisterTypes
方法中注册依赖项

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IBaseClass, BaseClass>(); // Registering types
    container.LoadConfiguration();
}
公共静态无效注册表类型(IUnityContainer容器)
{
container.RegisterType();//注册类型
container.LoadConfiguration();
}
除非您有完全不同的需求,否则您不需要创建任何自定义类型或实现


这将帮助您解决问题。

您使用的是Unity.Mvc吗?我通过nuget安装了Unity,我不确定@Chetanranpariya对于MVC和WebAPI项目,需要Unity.MVC。您甚至不需要创建自定义DependencyResolver。安装Unity.Mvc并在google中查找示例。配置和插件都很容易。我会试试这个,然后再给你回复。我很高兴它帮助你解决了这个问题。我刚刚发布了下面的答案。谢谢。我会接受这一点,因为它让我意识到我需要实现unity for web api,而不是mvc,因为我只使用web api。
public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IBaseClass, BaseClass>(); // Registering types
    container.LoadConfiguration();
}