嗨,我在启用自定义成员资格和角色提供程序时出现了一些问
在执行当前Web请求期间发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
System.InvalidOperationException:找不到默认成员资格提供程序.
它指向我的mvc3.ninject kernel.Inject(Membership.Provider)
我在app_start中的MVC3.Ninject:
private static void RegisterServices(IKernel kernel)
{
kernel.Inject(Membership.Provider);
kernel.Inject(Roles.Provider);
kernel.Bind<IUnitOfWork>().To<EFContext>().InRequestScope();
kernel.Bind<IUzytkownicyRepository>().To<UzytkownicyRepository>().InRequestScope();
}
Run Code Online (Sandbox Code Playgroud)
我的角色提供者:
public class DziennikRoleProvider : RoleProvider
{
[Inject]
private IUzytkownicyRepository repository { get; set;}
Run Code Online (Sandbox Code Playgroud)
我的会员提供者:
public class DziennikMembershipProvider : MembershipProvider
{
[Inject]
private IUzytkownicyRepository repository { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的WebConfig文件:
<membership defaultProvider="DziennikMembershipProvider">
<providers>
<clear/>
<add name="DziennikMembershipProvider" type="Dziennik_MVC.Helpers.DziennikMembershipProvider, Dziennik_MVC" connectionStringName="EFDbContext"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
</providers>
</profile>
<roleManager defaultProvider="DziennikRoleProvider" enabled="true" cacheRolesInCookie="true">
<providers>
<clear />
<add name="DziennikRoleProvider" type="Dziennik_MVC.Helpers.DziennikMembershipProvider, Dziennik_MVC" connectionStringName="EFDbContext" />
</providers>
</roleManager>
Run Code Online (Sandbox Code Playgroud) 我正在使用 asp.net MVC 2 和 Ninject 2。
设置非常简单。控制器调用调用存储库的服务。
在我的控制器中,我使用注入来实例化服务类,没有任何问题。但服务类不会实例化存储库,从而给我 NullReferenceException。
public class BaseController : Controller
{
[Inject]
public IRoundService roundService { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这有效。但这并不...
public class BaseService
{
[Inject]
public IRoundRepository roundRepository { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在 RoundService 类中使用 roundRepository 时,出现 NullReferenceException。
IList<Round> rounds = roundRepository.GetRounds( );
Run Code Online (Sandbox Code Playgroud)
模块类 -
public class ServiceModule : NinjectModule
{
public override void Load( )
{
Bind<IRoundService>( ).To<RoundService>( ).InRequestScope( );
}
}
public class RepositoryModule : NinjectModule
{
public override void Load( …Run Code Online (Sandbox Code Playgroud) 我刚刚将Ninject的现有实现从1.5升级到2.0.我现在看到在短时间内发生许多请求时会出现间歇性异常.
这是抛出的异常.
类型: System.ArgumentException
消息:已添加具有相同键的项.
来源: Ninject
堆栈跟踪: 在System.ThrowHelper.ThrowArgumentException(ExceptionResource资源)
在System.Collections.Generic.Dictionary`2.Insert(TKEY的关键,TValue值,布尔加)
在System.Collections.Generic.Dictionary`2.Add (TKEY的键,TValue值)
在Ninject.Components.ComponentContainer.CreateNewInstance(类型分量类型的实现)
在Ninject.Components.ComponentContainer.ResolveInstance(类型分量类型的实现)
在Ninject.Components.ComponentContainer.Get(类型分量)
在Ninject.Components.ComponentContainer <CreateNewInstance> b__6(的ParameterInfo参数)
在System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
在System.Linq.Buffer`1..ctor(IEnumerable`1源)
在System.Linq的.inumerable.ToArray [TSource](IEnumerable`1 source)
位于Ninject.Compone上的Ninject.Compone上的Ninject.Components.Component.CreateNewInstance(类型组件,类型实现)
Ninject.Compone上的Ninject.Compone上的Ninject.Components.Component.ResolveInstance(Type组件,类型实现)
. 在Ninject.KernelBase.CreateContext(IRequest请求,IBinding绑定)的
Ninject.Components.ComponentContainer.GetT 获取(类型组件
)
在Ninject.KernelBase.<> c__DisplayClassa.<解决> b__6(IBinding结合)
在System.Linq.Enumerable.<> c__DisplayClass12`3.<CombineSelectors> b__11(TSource X)
在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext ()
在System.Linq.Enumerable.<CastIterator> d__aa`1.MoveNext()
在System.Linq.Enumerable.Single [TSource](IEnumerable`1源)
在Ninject.ResolutionExtensions.Get [T](IResolutionRoot根,IParameter []参数)
在NameOfConsumingSite .Application_BeginRequest(Object sender,EventArgs e)...
如果我OnePerRequestModule从httpModulesweb.config中删除该项,则不会抛出异常,但这会增加大量开销.
如果他们可以提供帮助我提前感谢任何人!
我有一个Windows服务,其中包含一个文件监视器,当文件到达时会引发事件.当一个事件被引发时,我将使用Ninject来创建业务层对象,在它们内部引用了一个Entity Framework上下文,该上下文也是通过Ninject注入的.在我的Web应用程序中,我总是使用InRequestScope作为上下文,在一个请求中,所有业务层对象都使用相同的Entity Framework上下文.在我当前的Windows服务方案中,将实体框架上下文绑定切换到InThreadScope绑定是否足够?
理论上,当服务中的事件处理程序触发它在某个线程下执行时,如果另一个文件同时到达,它将在不同的线程下执行.因此,两个事件都不会共享实体框架上下文,实质上就像Web上的两个不同的http请求一样.
当你看Ninject wiki时,困扰我的一件事是破坏这些线程范围的对象:
.InThreadScope() - 每个线程将创建一个类型的实例.
.InRequestScope() - 将根据Web请求创建一个类型的实例,并在请求结束时销毁.
基于此,我了解InRequestScope对象将在请求结束时(或之后的某个时刻)被销毁(垃圾收集?).然而,这并没有说明InThreadScope对象是如何被销毁的.回到我的例子,当文件观察者事件处理程序方法完成时,线程消失(回到线程池?)注入的InThreadScope-d对象会发生什么?
编辑: 现在有一点很清楚,当使用InThreadScope()时,它不会在filewatcher的处理程序退出时销毁你的对象.我能够通过删除文件夹中的许多文件来重现这一点,并最终获得了相同的线程ID,这导致了与之前完全相同的Entity Framework上下文,因此对于我的应用程序来说,这绝对是不够的.在这种情况下,5分钟后出现的文件可能正在使用之前分配给同一线程的陈旧上下文.
我试图在网上找到如何通过Ninject使用AOP的例子.有人可以在不使用外部库(即Castle Windsor?)的情况下确认Ninject 2中是否有AOP可用.
如果可以,您是否可以发布资源链接以帮助我入门?
我有一个接口ISomething与方法Start.我想得到这个接口的所有实现(在多个程序集中,主要的和所有引用的),并在应用程序启动时调用Start方法.我怎么能用Ninject 2.2.0.0 .NET 4.0做到这一点?
Autofac答案在这里 调用Autofac中的所有ISomething实例
您好,我正在尝试使用注入实现全局过滤器.过滤器看起来像这样.
public class WikiFilter : IActionFilter
{
private IWikiService service;
public WikiFilter(IWikiService service)
{
this.service = service;
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
!!!Code here!!
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
我在global.asax中按照以下方式附加了过滤器.
public class MvcApplication : System.Web.HttpApplication,
IAuthenticationApplication<User>
{
protected void Application_Start()
{
Ninject();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
RegisterGlobalFilters(GlobalFilters.Filters);
}
private void Ninject()
{
// Create Ninject DI kernel
IKernel kernel = new StandardKernel();
kernel.Bind<DataContext>().ToSelf().InRequestScope();
kernel.Bind<IWikiRepository>().To<WikiRepository>();
kernel.Bind<IWikiService>().To<WikiService>();
// Global filters
kernel.BindFilter<WikiFilter>(FilterScope.Global, 0);
DependencyResolver.SetResolver
(new NinjectDependencyResolver(kernel));
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个抽象类:
public abstract class Validator<T> : IValidator
Run Code Online (Sandbox Code Playgroud)
以及为特定目的实现此类的几个类,例如
public sealed class NewsValidator : Validator<News>
Run Code Online (Sandbox Code Playgroud)
现在使用Ninject我想做如下的依赖注入(这个特定的代码不起作用):
Bind<Validator<News>>().To<NewsValidator>();
Bind(typeof(Validator<>)).To(typeof(NullValidator<>));
Run Code Online (Sandbox Code Playgroud)
所以我想要实现的是
Validator<News>
Run Code Online (Sandbox Code Playgroud)
应该绑定到类"NewsValidator",但如果要求此类的任何其他未绑定版本,请说
Validator<Article>
Validator<SomethingElse>
Run Code Online (Sandbox Code Playgroud)
应绑定到默认类(NullValidator).但是,使用上面使用的代码会抛出异常,因为它将Validator <News>绑定到NewsValidator以及NullValidator.
我怎么能实现这个?泛型类的特定类型应绑定到各个类.未明确绑定的所有其他类型的泛型类应绑定到默认类.
对于一些建议真的很高兴!谢谢!
我觉得我错过了一些明显的东西.我在这里阅读了几个相关的问题,我已经在Ninject的维基上阅读了更新的上下文绑定页面,但是它仍然不起作用.
我正在尝试改进使用工厂模式的遗留应用程序来使用Ninject.
我有1个接口(IInterface)由2个类(ClassB和ClassC)实现.IInterface有一个加载方法.在ClassB的加载方法中,它实例化ClassC然后执行它的加载方法.
基本上程序流是ClassA创建ClassB并执行load方法.在load方法中,ClassB创建了一些可以完成某些工作的ClassC.
我的绑定设置如此......
Bind<IInterface>().To<ClassC>().WhenInjectedInto<ClassB>();
Bind<IInterface>().To<ClassB>().WhenInjectedInto<ClassA>();
Run Code Online (Sandbox Code Playgroud)
当它运行时,它在ClassB的加载方法中失败并出现此错误...
激活IInterface时出错没有匹配的绑定可用,并且该类型不可自我绑定.
如果我尝试以下......
Bind<IInterface>().To<ClassC>().WhenInjectedInto<ClassB>();
Bind<IInterface>().To<ClassB>();
Run Code Online (Sandbox Code Playgroud)
它无限循环,永远不会创建ClassC.
编辑 我已将其简化为单元测试,但未给出我想要的结果...
[TestClass]
public class NinjectTestFixture
{
private interface IDoSomething
{
void SaySomething();
}
private class ClassA : IDoSomething
{
public void SaySomething()
{
Console.WriteLine("Hello from Class A");
}
}
private class ClassB : IDoSomething
{
private IKernel _Kernel;
public ClassB(IKernel kernel)
{
_Kernel = kernel;
}
public void SaySomething()
{
Console.WriteLine("Hello from Class B");
var x = _Kernel.Get<IDoSomething>();
x.SaySomething();
}
}
private class ClassC
{
private …Run Code Online (Sandbox Code Playgroud) 我有一个带有简单NinjectModule的ASP.NET MVC应用程序:
public class MainModule : NinjectModule
{
public override void Load()
{
Bind<AppSettings>().ToSelf().InSingletonScope();
Bind<HttpContext>().ToMethod(context => HttpContext.Current); // <-- problem
Bind<MainDbContext>().ToSelf().InRequestScope();
Bind<UserInfo>().ToSelf().InRequestScope();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我整个应用程序中唯一的绑定代码.当我运行我的应用程序时,我立即得到此运行时错误:
激活HttpContext时出错
可以使用多个匹配的绑定.
激活路径:
3)将依赖关系HttpContext注入到UserInfo类型的构造函数的参数httpContext中
2)将依赖关系UserInfo注入到HomeController类型的构造函数的参数userInfo中
1)HomeController的请求建议:
1)确保您只为HttpContext定义了一次绑定.
错误消息似乎是说我已经HttpContext多次定义了绑定,但是整个应用程序中唯一的绑定语句都在MainModule,并且我显然只定义了一个绑定HttpContext.如果我注释掉那行代码,我就会停止获取错误,但是HttpContext注入的错误是不正确的(它是一个空的,新实例化HttpContext而不是HttpContext.Current).
错误消息确实描述了我期望发生的确切注射顺序......
HttpContext应该注入到构造函数中UserInfo,如下所示:
public class UserInfo
{
private readonly HttpContext _httpContext;
public UserInfo(HttpContext httpContext)
{
_httpContext = httpContext;
}
// ... etc ... //
}
Run Code Online (Sandbox Code Playgroud)
并且UserInfo应该注入到构造函数中HomeController,如下所示:
public class …Run Code Online (Sandbox Code Playgroud) asp.net-mvc dependency-injection ninject ninject-2 asp.net-mvc-3
ninject-2 ×10
ninject ×8
c# ×3
asp.net ×2
asp.net-mvc ×2
ninject.web ×2
.net ×1
aop ×1
httpmodule ×1
roleprovider ×1