Ninject激活错误:没有匹配的绑定可用,并且类型不可自绑定

时间:2019-03-22 12:33:09

标签: c# wcf ninject

嗨,我正在尝试完成旧的WCF服务的升降,我遇到了一个错误。

基本上,我收到以下消息:

  

激活Searcher {Product}时出错,没有匹配的绑定可用,并且类型不可自绑定。激活路径:
    3)将依赖性Searcher {Product}注入到ProductSearchService类型的构造函数的参数productSearcher中
    2)将依赖项ProductSearchService注入到NinjectIISHostingServiceHost {ProductSearchService}类型的构造函数的参数实例中
    1)请求NinjectIISHostingServiceHost {ProductSearchService}

建议:

  1. 确保已为Searcher {Product}定义了绑定。
  2. 如果绑定是在模块中定义的,请确保已将模块加载到内核中。
  3. 确保您没有意外地创建了多个内核。
  4. 如果使用构造函数参数,请确保参数名称与构造函数参数名称匹配。
  5. 如果使用自动模块加载,请确保搜索路径和过滤器正确。

这里是Ninject绑定模块:

public override void Load()
{
   Bind<Searcher<Product>>().To<ProductWebHelpSearcher>().WhenInjectedInto<ProductSearchService>().InSingletonScope();
   Bind<Searcher<Product>>().To<ProductSearcher>().InRequestScope();
}

和NinjectWebCommon.cs的副本

public static class NinjectWebCommon
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start()
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Load<NinjectBindingsMappingModule>();
    }
}

最后是WCF服务的副本:

public class ProductSearchService : IProductSearchService
{
    private readonly Searcher<Product> _productSearcher;

    public ProductSearchService(Searcher<Product> productSearcher)
    {
        _productSearcher = productSearcher;
    }
}

有几个项目具有类似的设置,但我遇到的问题与此处不同,唯一的区别是这些应用程序是MVC而不是WCF。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在检查了亚历山大建议的名称空间之后,似乎svc .cs使用了一个名称空间,而Ninject映射模块被赋予了另一个名称空间。我现在使它们相同,而且一切正常。

相关问题