我们有一个具体的单件服务,它实现了Ninject.IInitializable2个接口.问题是服务Initialize-methdod被调用2次,只需要一次.我们使用的是.NET 3.5和Ninject 2.0.0.0.
Ninject中是否存在一种模式可以防止这种情况发生.两个接口都没有实现Ninject.IInitializable.服务类是:
public class ConcreteService : IService1, IService2, Ninject.IInitializable
{
public void Initialize()
{
// This is called twice!
}
}
Run Code Online (Sandbox Code Playgroud)
模块看起来像这样:
public class ServiceModule : NinjectModule
{
public override void Load()
{
this.Singleton<Iservice1, Iservice2, ConcreteService>();
}
}
Run Code Online (Sandbox Code Playgroud)
Singleton是一个定义如下的扩展方法:
public static void Singleton<K, T>(this NinjectModule module) where T : K
{
module.Bind<K>().To<T>().InSingletonScope();
}
public static void Singleton<K, L, T>(this NinjectModule module)
where T : K, L
{
Singleton<K, T>(module);
module.Bind<L>().ToMethod(n => n.Kernel.Get<T>());
}
Run Code Online (Sandbox Code Playgroud)
当然我们可以将bool …
我发现了大量关于如何使用Ninject在ASP.NET MVC3中对ActionFilter进行属性注入的不确定文章和问题.
有人能给我一个明确的例子吗?
这是我的自定义身份验证属性.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
//My custom code
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用WebActivator来设置Ninject
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Web.AppStart_NinjectMvc3), "Start")]
namespace MyProject.Web {
public static class AppStart_NinjectMvc3 {
public static void RegisterServices(IKernel kernel) {
//Binding things
}
public static void Start() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI …Run Code Online (Sandbox Code Playgroud) 我真的不知道从哪里开始这个问题,但我正在处理的网站有时会有一些非常慢的页面加载.特别是在完成构建之后,但并非总是如此.我通常必须在实际出现之前刷新页面5-10次.我想我正试图看看我应该从哪里开始看.
ASP.NET MVC 3 Ninject AutoMapper实体框架代码第一个4.1 SQL Server 2008 Razor
更新
关于一些问题,它可以在每个页面上进行长时间加载,但是在所有页面上加载相当快.
发布此并获得您的回复后,我启动了应用程序,它仍在加载,除非我在浏览器上单击重新加载,否则可能无法加载.
没有缓存,EF模型也不大.
我正在使用带有6 GB内存和I7处理器的Razor和Visual Studio 2010.
我正在调试时使用IIS Express和默认Web服务器.它也在主服务器上的IIS7上执行此操作.
我可以查看MVC Profiler和Glimpse,看看我能找到什么.
下面我有一些代码,当它到达主页时运行.我会说它在我第一次启动服务器时从不加载.我在var模型上设置了一个永不受到打击的断点.如果我重新加载页面,那么它.
public ActionResult Index()
{
var model = new HomeViewModel();
model.RecentHeadlines = _headlineService.GetHeadlines(1, Config.RecentHeadlinesPageSize, string.Empty);
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
下面是我的datacontext设置.
public class DatabaseFactory : Disposable, IDatabaseFactory
{
private DataContext _dataContext;
public DataContext Get()
{
return _dataContext ?? (_dataContext = new DataContext());
}
protected override void DisposeCore()
{
if (_dataContext != null)
_dataContext.Dispose();
}
}
public class Disposable : IDisposable …Run Code Online (Sandbox Code Playgroud) 多年来我一直在努力想到这一点.当我尝试用拦截器绑定我的类时,我在行上得到以下异常
Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>();
Run Code Online (Sandbox Code Playgroud)
加载Ninject组件IAdviceFactory时出错.没有这样的组件已在内核的组件容器中注册
我已尝试使用和不使用LoadExtensions,使用模块来设置我的绑定,我的上次尝试看起来像这样
internal class AppConfiguration
{
internal AppConfiguration( )
{
var settings = new NinjectSettings() { LoadExtensions = false };
Kernel = new StandardKernel(settings);
Load();
}
internal StandardKernel Kernel { get; set; }
public static AppConfiguration Instance
{
get { return _instance ?? (_instance = new AppConfiguration()); }
}
private static AppConfiguration _instance;
private void Load()
{
Kernel.Bind<ILoggerAspect>().To<Log4NetAspect>().InSingletonScope();
Kernel.Bind<MyClass>().ToSelf().Intercept().With<ILoggerAspect>();
}
internal static StandardKernel Resolver()
{
return Instance.Kernel;
}
}
Run Code Online (Sandbox Code Playgroud)
我的记录器属性看起来像这样
public class LogAttribute : InterceptAttribute
{
public override …Run Code Online (Sandbox Code Playgroud) 所以经过多次搞砸后,我终于将Ninject连接起来并在我的MVC4应用程序中进行编译.我遇到的问题是IDependencyScope接口不再存在,我所知道的并且System.Web.Http.Dependencies命名空间已经废除.
所以,我现在的问题是我已经连接了所有内容并运行我得到的应用程序:
Sequence contains no elements
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.Single(IEnumerable`1 source) +379
Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectMvcHttpApplicationPlugin.cs:53
Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:31
Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:53
Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:81
Run Code Online (Sandbox Code Playgroud)
我无法追踪甚至开始了解它的来源.
我在Global.asax.cs中的标准Ninject方法如下所示:
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<IRenderHelper>().To<RenderHelper>();
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
}
Run Code Online (Sandbox Code Playgroud)
我的定制解析器:
public class NinjectDependencyResolver : IDependencyResolver
{
private readonly IKernel _kernel;
public NinjectDependencyResolver(IKernel kernel)
{
_kernel …Run Code Online (Sandbox Code Playgroud) 我对WithConstructorArgument的理解可能是错误的,因为以下内容不起作用:
我有一个服务,让我们称之为MyService,其构造函数采用多个对象,以及一个名为testEmail的字符串参数.对于此字符串参数,我添加了以下Ninject绑定:
string testEmail = "test@example.com";
kernel.Bind<IMyService>().To<MyService>().WithConstructorArgument("testEmail", testEmail);
Run Code Online (Sandbox Code Playgroud)
但是,在执行以下代码行时,我得到一个异常:
var myService = kernel.Get<MyService>();
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
激活字符串时出错没有匹配的绑定可用,并且该类型不可自我绑定.激活路径:
2)将依赖字符串注入到MyService类型的构造函数的参数testEmail中
1)请求MyService建议:
1)确保已为字符串定义了绑定.
2)如果在模块中定义了绑定,请确保已将模块加载到内核中.
3)确保您没有意外创建多个内核.
4)如果使用构造函数参数,请确保参数名称与构造函数参数名称匹配.
5)如果使用自动模块加载,请确保搜索路径和过滤器正确无误.
我在这做错了什么?
更新:
这是MyService构造函数:
[Ninject.Inject]
public MyService(IMyRepository myRepository, IMyEventService myEventService,
IUnitOfWork unitOfWork, ILoggingService log,
IEmailService emailService, IConfigurationManager config,
HttpContextBase httpContext, string testEmail)
{
this.myRepository = myRepository;
this.myEventService = myEventService;
this.unitOfWork = unitOfWork;
this.log = log;
this.emailService = emailService;
this.config = config;
this.httpContext = httpContext;
this.testEmail = testEmail;
}
Run Code Online (Sandbox Code Playgroud)
我有所有构造函数参数类型的标准绑定.只有'string'没有绑定,而HttpContextBase的绑定有点不同:
kernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(new HttpContext(new MyHttpRequest("", "", "", …Run Code Online (Sandbox Code Playgroud) c# dependency-injection exception-handling ninject ninject-2
好的,我们有一个利用以下内容的开发基础架构:1.MVC 4(Razor视图)2.实体框架5代码优先3. Ninject
我正在寻找的是与我们现有基础设施尽可能接近的电子商务解决方案,希望尽可能无缝地集成.
我看了很多很多选项,包括:1.NopCommerce 2. Magelia 3. dashCommerce 4. DotShoppingcart
到目前为止,NopCommerce和Magelia(尤其是NopCommerce)似乎是我能找到的最接近的.我正在寻找可能更好地与我们上面提到的现有基础设施集成的其他电子商务解决方案的建议.成本确实不是问题,因此它可以是开源的或商业的.
欢迎任何和所有建议.
谢谢
缺口
为什么我不能在Ninect中执行以下操作?
Kernel.Bind<IPresenter>.To<DefaultPresenter>();
Kernel.Bind<IPresenter>.To<DashboardPresenter>();
Kernel.Bind<IPresenter>.To<HeartRatePresenter>();
Kernel.Bind<IPresenter>.To<GPSPresenter>();
Run Code Online (Sandbox Code Playgroud)
4个实现中的每一个都有不同的构造函数,期望不同的类型.当我尝试这个时,Ninject会抛出一个异常,告诉我我不能多次绑定到同一个界面.
在Presentable所有presenter类继承的类中,我试图在页面/视图中Kernel.Get<IPresenter>(new ConstructorArgument("view", this))分配IPresentable Presenter页面/视图,其中页面/视图实现了演示者期望作为参数的接口.
有什么方法可以让ninject识别不同的构造函数参数类型?
我在WebAPI项目中安装了以下软件包及其依赖项:
Ninject.Web.WebApi
Ninject.Web.WebApi.OwinHost
我这纯粹是作为web-api项目运行的.没有MVC.
当我运行我的应用程序并发POST送到AccountController的Register操作时,我得到以下错误:
{
"message":"An error has occurred.",
"exceptionMessage":"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.",
"exceptionType":"System.InvalidOperationException",
"stackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()",
"innerException":{
"message":"An error has occurred.",
"exceptionMessage":"Type 'RPT.Api.Controllers.AccountController' does not have a default constructor",
"exceptionType":"System.ArgumentException",
"stackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage …Run Code Online (Sandbox Code Playgroud) c# dependency-injection ninject asp.net-web-api asp.net-web-api2
我有一个使用Ninject的MVC 5应用程序,我正在添加Hangfire.
当我添加Ninject时,NinjectWebCommon由于其配置简单,我使用了nuget包.所以现在Ninject通过NinjectWebCommon创建标准内核的类来配置并添加绑定.
此外,我创建了一些我在创建内核时加载的自定义模块
private static IKernel CreateKernel() {
var kernel = new StandardKernel( new MyCustomModule() );
try {
kernel.Bind<Func<IKernel>>().ToMethod( ctx => () => new Bootstrapper().Kernel );
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices( kernel );
return kernel;
}
catch {
kernel.Dispose();
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
Ninject Web Common是通过WebActivatorEx课程注册的
[assembly: WebActivatorEx.PreApplicationStartMethod( typeof( MyProject.Web.NinjectWebCommon ), "Start" )]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute( typeof( MyProject.Web.NinjectWebCommon ), "Stop" )]
Run Code Online (Sandbox Code Playgroud)
现在问题与如何让Hangfire看到Ninject配置有关.通过查看Hangfire.Ninject我能读到的包裹
该包为IGlobalConfiguration接口提供了一个扩展方法:
var kernel = new StandardKernel();GlobalConfiguration.Configuration.UseNinjectActivator(kernel);
现在我的问题是:
IGlobalConfiguration接口,我应该在OWIN启动方法中添加Hangfire Ninject配置(已经放置了Hangfire配置).我应该如何获得当前的Ninject内核(NinjectWebCommon配置的内核)?WebActivatorEx …