ninject

Ninject caching an injected DataContext? Lifecycle Management?

China☆狼群 提交于 2019-11-28 19:56:46
I had a series of very bizarre errors being thrown in my repositories. Row not found or changed, 1 of 2 updates failed... Nothing made sense. It was as if my DataContext instance was being cached... Nothing made sense and I was considering a career move. I then noticed that the DataContext instance was passed in using dependency injection, using Ninject (this is the first time I have used DI...). I ripped out the Dependency Injection, and all went back to normal. Instantly. So dependency injection was the issue, but I still don't know why. I am speculating that Ninject was caching the injected

ASP.NET MVC2 + Ninject + NLog (+ shared hosting?) = NullReferenceException

别说谁变了你拦得住时间么 提交于 2019-11-28 19:17:32
问题 I have an MVC2 app that's based on the Tekpub Starter Site, so it uses Ninject for dependency injection, NLog for logging, and a bunch of other libraries in various places. As far as I can tell though, it's these that are causing my problem. Everything works beautifully on my PC using the ASP.NET dev server (Cassini) but when I deploy to the server (it's a cheap shared hosting deal), I get a NullReferenceException that seems to be related to Ninject instantiating the logger. Here's the

Using Ninject to fill Log4Net Dependency

时光怂恿深爱的人放手 提交于 2019-11-28 18:59:39
问题 I use Ninject as a DI Container in my application. In order to loosely couple to my logging library, I use an interface like this: 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 And my implementation looks like this public class Log4NetLogger : ILogger { private ILog _log; public Log4NetLogger(ILog log) { _log = log; } public void Debug(string message) {

MVC 3 - how to implement a service layer, do I need repositories?

懵懂的女人 提交于 2019-11-28 17:33:40
问题 I am currently building my first MVC 3 application, using EF Code First, SQL CE and Ninject. I have read a lot about using Repositories, Unit of Work and Service Layers. I think I have got the basics sorted out, and I have made my own implementation. This is my current setup: Entities public class Entity { public DateTime CreatedDate { get; set; } public Entity() { CreatedDate = DateTime.Now; } } public class Profile : Entity { [Key] public Guid UserId { get; set; } public string ProfileName

NInject with Generic interface

和自甴很熟 提交于 2019-11-28 16:05:25
I have defined one interface and one class: public interface IRepository<T> { } public class RoleRepository:IRepository<Domain_RoleInfo> { } Inject here: public RoleService { [Inject] public RoleService(IRepository<Domain_RoleInfo> rep) { _roleRep=rep; } } How can I perform Dependency Injection With Ninject,say how to bind? I have written a helper class as below, it works fine with non-generic interface.but how to refactor it support generic interface as above? public class RegisterNinjectModule : NinjectModule { public override void Load() { BindServices(); BindRepositories(); } private void

How to use Ninject in a multi-threaded Windows service to get new instances of a dependency (DbContext) on every tick?

£可爱£侵袭症+ 提交于 2019-11-28 14:25:06
I have inherited a Windows service where all the dependencies are created when the service starts and are injected in the transient scope. We are having a number of problems with this service, not least we have a DbContext which lives for the whole time the service is running, and different instances of it are injected each time. I would like to refactor so that each worker thread gets it’s own DbContext injected which will live for just the duration of each tick. I have looked at the custom scope . It looks fine for a single threaded app, but not multi-threaded. I also considered

Batch registering all implementations of a generic interface with Ninject

a 夏天 提交于 2019-11-28 12:52:42
i have the following interfaces injected in Castle Windsor. how do i do the same in Ninject? container.Register( AllTypes.FromAssemblyNamed("Apps.Web") .BasedOn(typeof(ICommandHandler<>)) .WithService.FirstInterface()); i've tried: this.Bind(x => x.FromAssembliesMatching("Apps.Web.dll") .Select(y => y.Namespace.EndsWith("Handlers")) .BindSingleInterface()); but getting Object reference not set to an instance of an object error. You can use Ninject's convention binding extensons (install it from NuGet ) to do this. Something like the following should work kernel.Bind(x => x

MVC5 Ninject binding and HttpContext

南楼画角 提交于 2019-11-28 12:51:38
I am trying to set up a new project and I've added a new class MembershipService that requires the HttpContext to be passed in it's constructor. In a previous project I used the code private static void RegisterServices(IKernel kernel) { kernel.Bind<IMembershipService>() .To<MembershipService>() .InRequestScope() .WithConstructorArgument("context", HttpContext.Current); .... } However in the new project I'm using Ninject Modules, and after some searching on StackOverflow and Google, I've come up with the code below: public class ServiceHandlerModule : NinjectModule { public override void Load(

Options for wiring dependencies with NInject

筅森魡賤 提交于 2019-11-28 12:24:53
With NInject (preferably 2.0), what options do we have wrt wiring up our object dependencies in a web application? Can they be defined in an XML configuration file? Or does it have to be done via code? Ninject doesn't have XML configuration, sorry but I can't provide a direct link (cos their site has flash elements), but here is a quotation from ninject.org : Free yourself from XML Most other .NET dependency injection frameworks are designed around the use of XML to declare type bindings. Rather than forcing you to write cumbersome and error-prone text, Ninject arms you with a fluent interface

Inject value into injected dependency

烂漫一生 提交于 2019-11-28 12:11:54
I'm having something like this: class Root { public Root(IDependency dep) {} } class Dependency:IDependency { public Dependency(int val) {} } And I'm trying to obtain a reference to Root using ninject. So i configure it like this var module = new InlineModule(mod => mod.Bind<IDependency>().To<Dependency>()); var kernel = new StandardKernel(module); I'd like to inject into Dependency some 'val' value that is known only at the moment of obtaining the Root reference from ninject. What i'd like to do is something like this: Kernel.Instance.Get<Root>(With.Parameters.ConstructorArgument("val", 12));