从Unity Container 2.0迁移到Unity容器5

时间:2017-12-08 14:56:21

标签: c# unity-container

所以,我的应用程序使用Unity Container v2进行依赖注入。现在我使用NUGET包将其迁移到5+,但是我遇到了有关类解析的问题。

var searchClient = IocContainer.Resolve<DocumentSearch>();

给我

The non-generic method 'IUnityContainer.Resolve(Type, string, params ResolverOverride[])' cannot be used with type arguments App.Super.Web.App D:\Repo\git1601\App.Super.Web.App\API\ApiControllers\DocumentsController.cs

我已经将导入从Microsoft.Pratices.Unity更改为Unity仅在包更改时,但仍然无效。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

这适用于从NuGet(5.3.2)下载的最后一个Unity

using Unity;

namespace ConsoleApplication1
{
    public class Foo
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            var f = container.Resolve<Foo>();
        }
    }
}

请注意Resolve<T>是一种扩展方法,虽然它在同一个命名空间(Unity)中定义,但您的编译器必须支持扩展方法。

您是否使用非常旧的C#2编译器编译它,其中扩展方法不可用?

另一个可能的原因是您的参考列表中没有Unity.Abstractions。请注意,虽然在UnityContainer程序集中定义了Unity.Container类型,但扩展方法是在另一个程序集中定义的(从NuGet安装,但安装两者)。

确保在您调用扩展方法的项目中引用这两个程序集,然后。

相关问题