Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 注入依赖项时出错_C#_Asp.net Mvc_Dependency Injection - Fatal编程技术网

C# 注入依赖项时出错

C# 注入依赖项时出错,c#,asp.net-mvc,dependency-injection,C#,Asp.net Mvc,Dependency Injection,我正在ASP.NET MVC中启动一个项目,使用Ninject实现依赖项注入。以下是我当前的架构: --数据层 存储库 public class AccountRepository : IAccountRepository { public bool Test(bool a) { return a; } } public interface IAccountRepository { bool Test(bool a); } i假设性 publi

我正在ASP.NET MVC中启动一个项目,使用Ninject实现依赖项注入。以下是我当前的架构:

--数据层

存储库

public class AccountRepository : IAccountRepository
{
    public bool Test(bool a)
    {
        return a;
    }
}
public interface IAccountRepository
{
    bool Test(bool a);
}
i假设性

public class AccountRepository : IAccountRepository
{
    public bool Test(bool a)
    {
        return a;
    }
}
public interface IAccountRepository
{
    bool Test(bool a);
}
----服务层

服务

IAccountRepository _IAccountRepository = null;
        public AccountService(IAccountRepository AccountRepository)
        {
            this._IAccountRepository = AccountRepository;
        }

        public bool test(bool a)
        {
            return _IAccountRepository.Test(a);
        }
iSeries设备

 public interface IAccountService
    {
        bool test(bool a);
    }
Ninject公共服务层

public static class NinjectWebCommon
{ 
    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IAccountRepository>().To<AccountRepository>();
    }        
}
IAccountService _IAccountService = null;

        public HomeController (IAccountService IAccountService)
        {
            this._IAccountService = IAccountService;
        }

        public ActionResult Index()
        {
            bool value = _IAccountService.test(true);
            return View();
        }
web层中的Ninject通用

   using System;
using System.Web;

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using Ninject;
using Ninject.Web.Common;
using Service.IService;
using Service.Service;
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();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IAccountService>().To<AccountService>();
    } 
使用系统;
使用System.Web;
使用Microsoft.Web.Infrastructure.DynamicModuleHelper;
使用Ninject;
使用Ninject.Web.Common;
使用Service.IService;
使用服务。服务;
公共静态类NinjectWebCommon
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
/// 
///启动应用程序
/// 
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestHttpModule));
RegisterModule(typeof(NinjectHttpModule));
初始化(CreateKernel);
}
/// 
///停止应用程序。
/// 
公共静态无效停止()
{
bootstrapper.ShutDown();
}
/// 
///创建将管理应用程序的内核。
/// 
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
尝试
{
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
注册服务(内核);
返回内核;
}
抓住
{
Dispose();
投掷;
}
}
/// 
///在这里加载您的模块或注册您的服务!
/// 
///内核。
私有静态无效注册服务(IKernel内核)
{
kernel.Bind().To();
} 
我得到以下错误

激活IAccountRepository时出错没有匹配的绑定 可用,且类型不可自绑定。激活路径:3) 将依赖关系库注入参数 AccountService 2)Injection类型构造函数的AccountRepository 将依赖项IAccountService转换为参数IAccountService的 类型为HomeController 1)的构造函数请求HomeController

建议:1)确保已为定义了绑定 IAccountRepository.2)如果绑定是在模块中定义的,请确保 确保模块已加载到内核中。3)确保 不会意外地创建多个内核。4)如果使用 构造函数参数,请确保参数名称与 构造函数参数名。5)如果您使用的是自动模块 加载时,请确保搜索路径和筛选器正确


请告诉我我做错了什么我已将ninject更新到最新版本,以防出现与版本相关的错误,因为错误消息表明类
AccountService
依赖于
AccountRepository
,因此您必须注入
AccountRepository

 private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IAccountRepository>().To<AccountRepository>();
        kernel.Bind<IAccountService>().To<AccountService>();
    }    
私有静态无效注册服务(IKernel内核)
{
kernel.Bind().To();
kernel.Bind().To();
}    

所以我需要在web层中提供数据层的引用。您的,这意味着它需要引用DI中涉及的所有库。就它而言,它只是将服务映射到抽象。它并不真正关心服务实际存在的位置。通常,您需要放置(至少部分)“将抽象转换为一个公共库,以便在不创建循环依赖项的情况下轻松地将其余层连接在一起。@blackwind090,请看以下内容:@yacoub感谢您的解释。您能提供一个工作层的链接吗?”example@blackwind090,您是指组合根与web pr分离的示例项目?我没有。我分享的答案只是解释了关于ASP.NET项目和合成根的混淆。你可以自己决定从web/合成根项目引用数据项目。但是现在你明白了为什么这样做了。