dependency-injection

Service Lifetimes in Blazor and Navigation

非 Y 不嫁゛ 提交于 2021-01-29 12:05:48
问题 Asumme this setup: Page1: - Component A - Component B Page2: - Component C - Component D Service: MyService I want Component A und B to get the same instance of MyService. Component C and D should get a fresh (and the same) instance of MyService. When I register MyService as transient each Component gets a fresh instance. When I register as singleton / scoped all components share the same instance. Components are part of library and thus I am looking for solution that requries no or very

Create database context from cookie and base path in Entity Framework Core

流过昼夜 提交于 2021-01-29 11:16:54
问题 Postgres database has multiple schemes like company1, company2, ... companyN Browser sends cookie containing scheme name . Data access operations should occur in this scheme. Web application user can select different scheme. In this case different cookie value is set. Npgsql EF Core Data provider is used. ASP NET MVC 5 Core application registers factory in StartUp.cs : public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddScoped

Dependency Injection on static method in ASP .Net Core

走远了吗. 提交于 2021-01-29 09:48:36
问题 I have registered the implementation of my logger in ServiceCollection in the start up: services.AddTransient(typeof(ILogger<>), typeof(GenericLogger<>)); Usually, I do this to inject using Constructor: class DynamoEventProcessor { private readonly IRepository _repository; private readonly IDogStatsd _dogStatsd; private readonly ILogger<DynamoEventProcessor> _logger; public DynamoEventProcessor(IRepository repository, IDogStatsd dogStatsd, ILogger<DynamoEventProcessor> logger) { _repository =

Register Open Generic with dotnet core 3

送分小仙女□ 提交于 2021-01-29 09:21:45
问题 I'm trying to register an Open Generic type in my application but I'm having a strange behavior. The following is working correctly: services.AddTransient(typeof(IA<>), typeof(A<>)); Now, I would like to pass parameters to my A class, because my constructor has some arguments: services.AddTransient(typeof(IA<>), provider => { return ActivatorUtilities.CreateInstance(provider, typeof(A<>), "1", "2"); }); This generates an exception 'Open generic service type 'IA`1[T]' requires registering an

How do I register components at run time with unity DI

萝らか妹 提交于 2021-01-29 08:23:13
问题 I created an Microsoft unity project (my first using Dependency Injection) with my home controller working fine as expected having registered my messaging Class as follows private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<IMessageService, MessageService>(new HierarchicalLifetimeManager()); RegisterTypes(container); return container; } The challenge is I intend making this project a parent to several modules that will be plugins

ASP.NET Core Multiple Implementation for Dependency Injection

不羁岁月 提交于 2021-01-29 08:07:17
问题 Is there a way I can designate multiple implementations of a single interface using ASP.NET Core? I could do this in Ninject like this: ninjectKernel.Bind<DbContext>().To<OracleDbContext>().Named("UnitWork"); ninjectKernel.Bind<DbContext>().To<AppsDbContext>().Named("AppsWork"); 回答1: If your question is specific to just DbContext then it's easy using the following statements public void ConfigureServices(IServiceCollection services) { services.AddDbContext<OracleDbContext>(builder => builder

Angular singleton service

大憨熊 提交于 2021-01-29 06:14:14
问题 From my understanding the only difference between using providedIn: 'root' and adding your provider to the providers array of the module is for tree shaking. Currently my application works fine if I add providedIn: 'root' it works fine, but if I remove that and add it to the providers array of the module I get the StaticInjectorError saying it cant find the provider. Has anyone seen this or have an understanding of why this would happen? From the docs I believe that adding it to the providers

Ambiguity Regarding Spring Constructor Injection

安稳与你 提交于 2021-01-29 00:15:44
问题 why am i able to see First Constructor even if i specified the type can anyone please explain me what is happening behind the scene... since i don't want to specify the index positions i need to call second constructor based on the type. public class Employee { String name; int id; public Employee(String name,int id) { System.out.println("First Constrcuot "); } public Employee(int id,String name){ System.out.println("Second Constrcuot "); } } I have My Beans.xml as follows: <bean id="employee

Ambiguity Regarding Spring Constructor Injection

只愿长相守 提交于 2021-01-29 00:10:57
问题 why am i able to see First Constructor even if i specified the type can anyone please explain me what is happening behind the scene... since i don't want to specify the index positions i need to call second constructor based on the type. public class Employee { String name; int id; public Employee(String name,int id) { System.out.println("First Constrcuot "); } public Employee(int id,String name){ System.out.println("Second Constrcuot "); } } I have My Beans.xml as follows: <bean id="employee

Ambiguity Regarding Spring Constructor Injection

旧街凉风 提交于 2021-01-29 00:07:56
问题 why am i able to see First Constructor even if i specified the type can anyone please explain me what is happening behind the scene... since i don't want to specify the index positions i need to call second constructor based on the type. public class Employee { String name; int id; public Employee(String name,int id) { System.out.println("First Constrcuot "); } public Employee(int id,String name){ System.out.println("Second Constrcuot "); } } I have My Beans.xml as follows: <bean id="employee