从 Unity Container 2.0 迁移到 Unity Container 5
Migrating from Unity Container 2.0 to Unity Container 5
所以,我的应用程序使用 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
只是因为包发生了变化,但仍然无法正常工作。有什么想法吗?
这适用于从 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 安装会安装两者)。
然后确保在您调用扩展方法的项目中引用了两个程序集。
所以,我的应用程序使用 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
只是因为包发生了变化,但仍然无法正常工作。有什么想法吗?
这适用于从 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 安装会安装两者)。
然后确保在您调用扩展方法的项目中引用了两个程序集。