我正在尝试将新的ASP.Net MVC 4 Web API项目模板与Ninject一起使用,但是在以下错误中遇到了障碍:
从程序集'Ninject.Web.WebApi,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = c7192dc5380945e7'类型'Ninject.Web.WebApi.Filter.DefaultFilterProvider'中的方法'GetFilters'没有实现.
我正在使用ASP.Net MVC 4 - > Web API模板在Visual Studio 2010中创建一个全新的项目,我正在使用最新的Ninject NuGet包:
我尝试了这个问题中提出的解决方案,但是我没有运气 - 如果我删除对Ninject.Web.WebApi的引用,那么MVC永远不会使用Ninject.我也注意到他们提到了Ninject.MVC3但是我使用的是新的Ninject.WebApi插件.
我正在使用NinjectWebCommon.cs中的默认绑定代码,该代码是在NuGet安装期间创建的,并尝试在RegisterServices()中注册一个简单服务
[assembly: WebActivator.PreApplicationStartMethod(typeof(mkts.web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(mkts.web.App_Start.NinjectWebCommon), "Stop")]
namespace mkts.web.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using mkts.service;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts …Run Code Online (Sandbox Code Playgroud) 我很惊讶地发现Ninject创建的至少一个对象在请求结束时没有被处理掉,当它被定义为InRequestScope时
这是我试图处理的对象:
接口:
public interface IDataContext : IDisposable
{
MessengerEntities context { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
MessengerEntities是Entity Framework的ObjectContext实现 - 我的上下文对象.
然后我创建一个具体的类,如下所示:
public class DataContext : IDataContext
{
private MessengerEntities _context = new MessengerEntities();
public MessengerEntities context
{
get
{
return _context;
}
set
{
_context = value;
}
}
#region IDisposable Members
public void Dispose()
{
context.Dispose();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个像这样的Ninject控制器工厂(这是以史蒂夫桑德森MVC 2书为蓝本):
public class NinjectControllerFactory : DefaultControllerFactory
{
// a Ninject "kernel" is the thing that can supply object instances …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用Ninject作为DI容器.为了松散地耦合到我的日志库,我使用这样的接口:
public interface ILogger
{
void Debug(string message);
void Debug(string message, Exception exception);
void Debug(Exception exception);
void Info(string message);
...you get the idea
Run Code Online (Sandbox Code Playgroud)
我的实现看起来像这样
public class Log4NetLogger : ILogger
{
private ILog _log;
public Log4NetLogger(ILog log)
{
_log = log;
}
public void Debug(string message)
{
_log.Debug(message);
}
... etc etc
Run Code Online (Sandbox Code Playgroud)
具有日志记录依赖性的示例类
public partial class HomeController
{
private ILogger _logger;
public HomeController(ILogger logger)
{
_logger = logger;
}
Run Code Online (Sandbox Code Playgroud)
在实例化Log4Net的实例时,您应该为其指定要记录的类的名称.这对Ninject来说是一个挑战.
目标是在实例化HomeController时,Ninject应该使用"HomeController"的"名称"实例化ILog
这是我的配置
public class LoggingModule : NinjectModule
{
public override void …Run Code Online (Sandbox Code Playgroud) 我有一些工作经验Structuremap.现在我用大造和大型Web应用程序(一个真正的社交网站)ASP.NET MVC 3,Entity Framework 4.1 code-first,SqlServer 2008,通过C#.NET 4.哪一个Structuremap,AutoFac和Ninject你会建议我在这种情况下使用?性能和易用性很重要.感谢任何帮助,想法和建议.
我们在所有项目中使用ninject,正如您所知,有时候很难测试内核是否能够在执行时解析每个类型,因为有时控件会在绑定和自动绑定的大小时丢失(通过ninject扩展) ) 高.
所以,我在这里问的是,我怎么知道我的内核在加载所有模块和绑定后,能够解析每种类型?你做过任何单元测试吗?或者您只是在执行时对应用程序进行接受测试?任何建议都会很棒:)
我正在使用Ninject从内核中检索我的DataContext,我想知道Ninject是否自动处理DataContext,或者他如何处理dispose()行为.根据自己的经验,我知道处理datacontext非常重要,每当你创建DataContext的直接对象时(如:new DataContext()),你应该使用using()块.
我的问题是:当我从内核中检索我的DataContext时,我是否仍然必须使用using()块?或者Ninject是否为我解决了这个问题?
我如何在Ninject语法中使用多个参数,如下所示?
Bind<IMyRepository>()
.To<SqlMyRepository>()
.WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString
);
Run Code Online (Sandbox Code Playgroud)
如果需要传递多个参数怎么办?
好吧,这个问题很简单地用标题来说明.
对于局部变量factory:
var factory = Fluently.Configure()
...
Run Code Online (Sandbox Code Playgroud)
这两行是否相同:
Bind<ISessionFactory>().ToConstant(factory).InSingletonScope();
Run Code Online (Sandbox Code Playgroud)
和:
Bind<ISessionFactory>().ToConstant(factory);
Run Code Online (Sandbox Code Playgroud) 如果我有以下代码:
public class RobotNavigationService : IRobotNavigationService {
public RobotNavigationService(IRobotFactory robotFactory) {
//...
}
}
public class RobotFactory : IRobotFactory {
public IRobot Create(string nameOfRobot) {
if (name == "Maximilian") {
return new KillerRobot();
} else {
return new StandardRobot();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在这里做反转控制的正确方法是什么?我不想将KillerRobot和StandardRobot混凝土添加到Factory类吗?而且我不想通过IoC.Get <>带来它们吗?那个服务地点不是真的IoC对吗?有没有更好的方法来解决在运行时切换混凝土的问题?
目前正在编写API站点(.NET Web Api 2.1)
对于我们以前的API站点,我们使用了Ninject.MVC3包并手动连接了依赖项解析器和范围,并根据建议将我们的逻辑插入到NinjectWebCommon中.
这导致我们的新项目出现胃灼热,无参数构造函数错误.在过去,这是一个死的赠品,我们没有正确地连接Web Api中的依赖性解析器.只有这一次,我们是.它就在那里.
var resolver = new NinjectDependencyResolver(kernel);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
Run Code Online (Sandbox Code Playgroud)
所以我有点失落.在上周,我看到Ninject已更新,WebAPI nuget包已复活,因此我决定尝试实施.
我安装了NuGet包Ninject.Web.WebApi(版本3.2).这似乎不包括应用绑定的NinectWebCommon.cs文件.它只添加了必要的组件.做一些挖掘我偶然发现了这个实现.
https://gist.github.com/odytrice/5842010
(我不清楚Nuget包是否正在为我做这个,这都是冗余代码.如果是这样,那很好,我只需要了解如何使用nuget包添加我们的绑定.)
除了加载模块之外,我在线上实现了这一行,我将三个单独的模块加载为测试.
public static INinjectModule[] Modules
{
//get { return new INinjectModule[] { new MainModule() }; }
get { return new INinjectModule[] { new GlobalModule(), new InventoryModule(), new StoreModule() }; }
}
Run Code Online (Sandbox Code Playgroud)
我已经验证了使用这个新代码设置了依赖项解析器.
_resolver = new NinjectHttpResolver(modules);
GlobalConfiguration.Configuration.DependencyResolver = _resolver;
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在.
ExceptionMessage:
An error occurred when trying to create a controller of type 'StoreSearchController'.
Make sure that the controller has a …Run Code Online (Sandbox Code Playgroud) ninject ×10
c# ×8
.net ×2
asp.net-mvc ×1
autofac ×1
datacontext ×1
dispose ×1
idisposable ×1
log4net ×1
structuremap ×1