ninject

How to inject UserManager & SignInManager

北城以北 提交于 2019-11-27 11:50:31
I am trying to figure out how to inject UserManager and SignInManager. I have installed Ninject in my application and I am using it in the following manner: Please consider this to be a brand new project. Inside Startup.cs I have the following: public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); app.UseNinjectMiddleware(CreateKernel); } private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Load(Assembly.GetExecutingAssembly()); return kernel; } } now if I were to create some Dummy class and try to inject it based on its

How to use DI container when OwinStartup

a 夏天 提交于 2019-11-27 11:31:59
It's a Web API 2 project. When I implement DI using Ninject, I got an error message An error occurred when trying to create a controller of type 'TokenController'. Make sure that the controller has a parameterless public constructor. [assembly: OwinStartup(typeof(Web.Startup))] namespace Web { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); ConfigureWebApi(app); } } } public class TokenController : ApiController { private IUserService _userService; public TokenController(IUserService userService) { this._userService = userService; } [Route("api

How to integrate Ninject into ASP.NET Core 2.0 Web applications?

怎甘沉沦 提交于 2019-11-27 09:05:30
I have found out that Ninject has recently introduced support for .NET Standard 2.0 / .NET Core 2.0 . However, I cannot find any extension to actually integrate it in the Web application (e.g similar to Ninject.Web.Common ) Looking on the code from an old ASP.NET MVC solution, I realized that the whole mechanism is different as the classic one relied on WebActivatorEx.PreApplicationStartMethod and WebActivatorEx.ApplicationShutdownMethodAttribute which are no longer available in ASP.NET Core. Also, the old Ninject.Web.Common assembly provided several useful classes used for initialization -

Guidelines For Dispose() and Ninject

ぐ巨炮叔叔 提交于 2019-11-27 09:00:52
So, I have a method exposed from a WCF service as such: public GetAllCommentsResponse GetAllComments(GetAllCommentsRequest request) { var response = new GetAllCommentsResponse(); using(_unitOfWork) try { Guard.ArgNotNull(request, "request"); var results = _unitOfWork.CommentRepository.Get(d => d.Id > 0).ToArray(); //... Do rest of stuff here } catch (Exception ex) { response.Success = false; response.FailureInformation = ex.Message; Logger.LogError("GetAllComments Method Failed", ex); } return response; } I have a global DataUnitOfWork object (which implements IDisposable) that gets

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

走远了吗. 提交于 2019-11-27 08:29:52
问题 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

Ninject + Bind generic repository

萝らか妹 提交于 2019-11-27 08:11:31
I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null? I have tried various things like: Bind(typeof(IRepository<CustomerModel>)).To(typeof(Repository<CustomerModel>)); Bind(typeof(IRepository<>)).To(typeof(Repository<>)); However if I pass in a non-generic interface and class then it works like a dream? quentin-starin Bind(typeof(IRepository<>)).To(typeof(Repository<>)); This is the correct syntax for binding an open generic. If you are receiving null back when requesting IRepository< of whatever > , then there may be some other

How to handle DBContext when using Ninject

↘锁芯ラ 提交于 2019-11-27 08:03:11
I am trying to use Ninject and OpenAccess for the first time. Please help me with the following. Here is what my project looks like... public class ContentController : Controller { private ContentService contentSvc; public ContentController(ContentService contentSvc) { this.contentSvc = contentSvc; } } The following class is under a folder in my web app. public class ContentService { private IContentRepository contentRepository; public ContentService(IContentRepository contentRepository) { this.contentRepository = contentRepository; } public void InsertContent(Content content) {

Batch registering all implementations of a generic interface with Ninject

风格不统一 提交于 2019-11-27 07:23:52
问题 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. 回答1: You can use Ninject's convention binding extensons (install

MVC5 Ninject binding and HttpContext

此生再无相见时 提交于 2019-11-27 07:22:07
问题 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

Lazy<> Ninject Injection

纵然是瞬间 提交于 2019-11-27 06:57:28
问题 I use ninject framework. In my code I have a Lazy object. I can create an instance, but when I call the value property I got an exception. private Lazy<IPsoriasisReportUserControl> psoriasisReportUserControl; [Inject] public Lazy<IPsoriasisReportUserControl> PsoriasisReportUserControl { get { return psoriasisReportUserControl; } set { psoriasisReportUserControl = value; } } I got The lazily-initialized type does not have a public, parameterless constructor exception because the injection does