gpt4 book ai didi

c# - 注入(inject) ActivationException : Error activating IAlertManagement

转载 作者:太空狗 更新时间:2023-10-29 19:47:17 25 4
gpt4 key购买 nike

我收到以下错误:

Test method: BootStrapperTest.Can_Create_Alert_Management_Object threw exception:  Ninject.ActivationException: 
Error activating IAlertManagement No matching bindings are available, and the type is not self-bindable.

Activation path:
1) Request for IAlertManagement

Suggestions:
1) Ensure that you have defined a binding for IAlertManagement.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.

这是导致此异常的测试用例:

[TestInitialize]
public void Initialize()
{
BootStrapper.RegisterTypes();
}

[TestMethod]
public void Can_Create_Alert_Management_Object()
{
IAlertManagement alertManagementService = BootStrapper.Kernel.Get<IAlertManagement>();

Assert.IsNotNull(alertManagementService);
}

//This is the code that gets called in [TestInitialize]
public static void RegisterTypes()
{
if (!initialized)
{
Kernel.Bind(scanner => scanner.FromAssembliesMatching("MyCompany.MyProduct.*")
.SelectAllClasses()
.BindDefaultInterface());

Kernel.Unbind(typeof(IWcfServiceClient<>));
Kernel.Bind(typeof(IWcfServiceClient<>)).ToMethod(ctx =>
(ctx.Kernel.Get(typeof(WcfServiceClientProvider<>).MakeGenericType(ctx.GenericArguments)) as IProvider).Create(ctx));
}

initialized = true;
}

上述错误发生在我在构建服务器 上进行的单元测试中,但未发生在我的开发机器上。我有 7 个其他测试几乎与此相同,它们在构建服务器和我的开发机器上通过,但这是唯一失败的测试。

IAlertManagement 接口(interface)来自一个名为 Core 的 dll,具体类型来自另一个名为 AlertManagement 的 dll。我的单元测试项目中包含 Core dll 和 AlertManagement dll 作为项目引用。我有 7 或 8 个其他测试与这种情况相同,但这是唯一一个失败的测试。

如有任何想法,我们将不胜感激。

最佳答案

错误发生是因为 IAlertManagement 没有与任何具体类绑定(bind)。尝试手动绑定(bind) IAlertManagement

public static void RegisterTypes()
{
if (!initialized)
{
Kernel.Bind(scanner => scanner.FromAssembliesMatching("MyCompany.MyProduct.*")
.SelectAllClasses()
.BindDefaultInterface());


//Try to add following line
Kernel.Bind<IAlertManagement>().To<ConcreteImplementationOfIAlertManagement>();

Kernel.Unbind(typeof(IWcfServiceClient<>));
Kernel.Bind(typeof(IWcfServiceClient<>)).ToMethod(ctx => (ctx.Kernel.Get(typeof(WcfServiceClientProvider<>).MakeGenericType(ctx.GenericArguments)) as IProvider).Create(ctx));
}

initialized = true;
}

关于c# - 注入(inject) ActivationException : Error activating IAlertManagement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11864279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com