ninject

Ninject : Constructor parameter

狂风中的少年 提交于 2019-11-29 15:03:57
问题 I am using Ninject together with ASP.NET MVC 4. I am using repositories and want to do constructor injection to pass in the repository to one of the controllers. This is my Repository interface: public interface IRepository<T> where T : TableServiceEntity { void Add(T item); void Delete(T item); void Update(T item); IEnumerable<T> Find(params Specification<T>[] specifications); IEnumerable<T> RetrieveAll(); void SaveChanges(); } The AzureTableStorageRepository below is an implementation of

MVC5, WebAPI2 and Ninject Parameterless constructor error

岁酱吖の 提交于 2019-11-29 14:50:59
So I have the exact opposite problem as MVC5, Web API 2 and Ninject I have a new MVC5/WebAPI2 project, that has both "Controller"s and "ApiControllers". I'm using the latest unstable version of Ninject.Web.WebAPI with no code changes to NinjectDependencyResolve.cs and Ninject.WebCommom.cs (besides binding my dependency) the ApiController's constructor injection works. However, when I call a MVC Controller I get: No parameterless constructor defined for this object. Tim The issue is you need a Dependency Resolver for both MVC and WebAPI. Depending on which set of Ninject libraries you use, you

Injecting the Dependency Injector using Dependency Injection

a 夏天 提交于 2019-11-29 14:49:06
问题 Pretty new to dependency injection and I'm trying to figure out if this is an anti pattern. Let's say I have 3 assemblies: Foo.Shared - this has all the interfaces Foo.Users - references Foo.Shared Foo.Payment - references Foo.Shared Foo.Users needs an object that is built within Foo.Payment, and Foo.Payment also needs stuff from Foo.Users. This creates some sort of circular dependency. I have defined an interface in Foo.Shared that proxies the Dependency Injection framework I'm using (in

How To Use Ninject Named Bindings With DependencyResolver and PropertyInjection

一个人想着一个人 提交于 2019-11-29 14:48:21
问题 I realize constructor injection is preferred but I'm curious how to use Ninject's contextual 'Named Bindings' when using another form of injection. Specifically how do I do the following when using DependencyResolver or property injection. public MyService([Named("Alpha")] IRepository repository) { this.repository = repository; } 回答1: You can create a named binding to work on Alpha: Bind<IRepository>().To<AlphaRepository>().Named("Alpha"); then you can specify others like: Bind<IRepository>()

Ninject w/ ASMX web service in a MVC3/Ninject 3 environment

纵饮孤独 提交于 2019-11-29 14:47:28
I'm looking for the best way to incorporate a classic asmx web service into my MVC3 environment. I'd like the kernel/container to be shared between the two. In the past with ASP.NET web forms I was able to use the following: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class Service : WebServiceBase { [Inject] public IContextProvider ContextProvider { get; set; } ... This used the older Ninject.Web to shared the kernel using the KernelContainer that was shared between WebServiceBase, PageBase, etc. etc. Now I've got a MVC3

Ninject Scope issue with Tasks/Threads

此生再无相见时 提交于 2019-11-29 13:59:05
I have an MVC3 project that uses Ninject, Entity Framework and the Unit of Work pattern with a Service layer. My AsyncService class has a function that starts a background task that, as an example, adds users to the User repository. My current problem is that the task only runs correctly for a few seconds before I get an error that the DbContext has been disposed. My database context, which is injected with Ninject's InRequestScope() seems to be getting disposed, as InRequestScope() ties it to HttpContext. I've read about InThreadScope(), however I'm not sure how to implement it properly in my

Getting 'Context is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.\"

旧城冷巷雨未停 提交于 2019-11-29 13:30:19
I am getting this error when I try to use code first migrations. My context has a constructor with the connection name. public class VeraContext : DbContext, IDbContext { public VeraContext(string NameOrConnectionStringName = "VeraDB") : base(NameOrConnectionStringName) { } public IDbSet<User> Users { get; set; } public IDbSet<Product> Products { get; set; } public IDbSet<IntCat> IntCats { get; set; } } This connection name is injected with ninject when the project runs, I have also specified it as a default as in the above code but this did not help. kernel.Bind<IDbContext>() .To<VeraContext>

WebApi Custom Filter with injected interface in constructor does not get called

坚强是说给别人听的谎言 提交于 2019-11-29 12:45:54
My problem statement is same as this question i.e., use an injected service in attribute/filter. I have tried the solution given by B Z and following is my code along the lines of solution given. //marker attribute public class AuthorizeViewAttribute : Attribute { } //filter public class AuthorizeViewFilter : IAuthorizationFilter { private readonly IAccessRightsService _iAccessRightService; public AuthorizeViewFilter(IAccessRightsService iAccessRightService) { _iAccessRightService = iAccessRightService; } public void OnAuthorization(AuthorizationContext filterContext) { RoleFeature roleFeature

How to configure Ninject for MVC4 & custom Membership provide?

人盡茶涼 提交于 2019-11-29 12:14:42
According to this article description custom-membership-provider-with-repository-injection I implement the custom Membership provide with inject. Custom Membership provider using Ninject; public class CustomMembershipProvider : MembershipProvider { [Inject] public IUserRepository UserRepository { get; set; } [...] Custom Role Provider using Ninject; public class CustomRoleProvider : RoleProvider { [Inject] public IUserRoleRepository UserRoleRepository { get; set; } [...] within Web.Config <membership defaultProvider="CustomsMembershipProvider"> <providers> <clear/> <add name=

How to implement an [GoF]-ish Abstract Factory Pattern using an IoC like Ninject

老子叫甜甜 提交于 2019-11-29 12:07:49
Abstract When the design requires an "Abstract Factory Pattern" like stated by the [GoF] including several products and over some product families, then setting up an IoC can become a bit tricky. Especially when the specific factory implementations need to be dispatched by runtime parameters and shared among some subsequent components. Given the follwing API, i was trying to set up my IoC (Ninject in this case) to retrieve Configuration objects configured through a IConfigurationFactory . The configuration stores an IFactory instance whoose implementation is determined by a runtime parameter