Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# 在ASP.NET3.5中使用Ninject 2.0_C#_Dependency Injection_Inversion Of Control_Ninject - Fatal编程技术网

C# 在ASP.NET3.5中使用Ninject 2.0

C# 在ASP.NET3.5中使用Ninject 2.0,c#,dependency-injection,inversion-of-control,ninject,C#,Dependency Injection,Inversion Of Control,Ninject,我正在尝试将Ninject 2.0与Asp.NET3.5Web应用程序结合使用。以下是我正在使用的DLL及其版本 Ninject.dll-v2.0.0.0 Ninject.Extensions.Logging.dll v2.0.0.0 Ninject.Web.dll v1.0.0.0 在我的global.ascx.cs中,我有以下方法 protected override IKernel CreateKernel() { IKernel kernel = new St

我正在尝试将Ninject 2.0与Asp.NET3.5Web应用程序结合使用。以下是我正在使用的DLL及其版本

  • Ninject.dll-v2.0.0.0
  • Ninject.Extensions.Logging.dll v2.0.0.0
  • Ninject.Web.dll v1.0.0.0
在我的global.ascx.cs中,我有以下方法

protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel();            
        kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope();
        return kernel;
    }

我不明白,即使我没有试图注册记录器,它似乎是试图创建自己的。如何解决此错误?我必须使用Ninject的任何扩展记录器吗

谢谢
GK

该错误表明Ninject正在尝试将
iLogger工厂
接口解析为具体类。根据您在上面提到的参考资料,您的ASP.NETWeb应用程序似乎是基于WebForms的,而不是ASP.NETMVC应用程序

考虑到这一点,您的页面应该派生自
PageBase
,它是Ninject web库提供的一个抽象类。该类具有以下属性定义:

[Inject]
public ILogger Logger { get; set; }
因此,当您的页面被实例化时,Ninject试图将记录器注入页面上的属性中。正如错误所暗示的,您需要为
ILogger工厂
以及
ILogger
定义绑定;但是,您提供的唯一绑定是针对
IDataAccess
。您还需要加载日志扩展库中定义的一个模块。我相信您可以在NLog和Log4Net之间进行选择。例如,如果您想使用Log4Net,您的
CreateKernel
函数如下所示:

protected override IKernel CreateKernel()
{
    IKernel kernel = new StandardKernel(new Log4netModule());            
    kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope();
    return kernel;
}
protectedoverride IKernel CreateKernel()
{
IKernel kernel=新的标准内核(新的Log4netModule());
kernel.Bind().To().InSingletonScope();
返回内核;
}
这假设您有一个使用Ninject.Extensions.Logging.Log4net的
语句


在任何一种情况下,您都必须选择一个记录器,并根据Ninject Web库的需要加载它的绑定(通过模块)。如果您现在没有任何日志问题,您可以选择提供不做任何事情的
ILogger工厂
ILogger工厂
的实现(即“null”记录器),并自己绑定虚拟ILogger工厂。

我也有同样的问题。 再次尝试引用DDL log4net.dll、ninject.extensions.logging.dll、ninject.extensions.log4net.dd、ningject.web.dll和ninject.dll 这解决了我的问题。最可能是由于旧的log4net.dll。

请修复格式设置(缺少泛型类型,请使用预览!)。第一个猜测是寻找建议2。。
protected override IKernel CreateKernel()
{
    IKernel kernel = new StandardKernel(new Log4netModule());            
    kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope();
    return kernel;
}