Questions tagged [servicecollection]

28 questions
11
votes
2 answers

'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:IHostedService

I am using .NET 5 and I want to run a background task using IHostedService classess as you can see: public class PasargadJobs : IHostedService, IDisposable { private Timer _timer = null!; readonly ITerminalService _terminalService; …
4
votes
2 answers

Options pattern - Interface/Abstract property

I want to bind the following object with the appsettings.json data in ServiceCollection, however I cannot change the design of the class or the interface: public class TransferOptions :ITransferOptions { public IConnectionInfo Source {get;set;} …
2
votes
1 answer

.NET Services collection life time

is it possible to register some services later when the application up and running ? I stuck into a scenario like this I have WPF or MAUI application that use signalR for realtime communication, the problem is the application requirement doesn't…
2
votes
1 answer

.NET Core dependency injection - How to dispose manually created instances?

Like the below code, I have to use the factory/func way of registration with container as I don't know the value of "userId" property ahead of time but only during runtime. This is working & no issues with functionality…
2
votes
1 answer

C# How do you build up a collection in the IServiceCollection across library boundaries?

I inject a generic list List through a constructor. Users of my library should be able to add their own implementations of ICommand. All of the ICommand implementations are known at compile time. Currently, I add to the ServiceCollection…
1
vote
3 answers

IOptions returns null despite GetSection working

I'm currently trying to read appsettings.json values into a variable. GetSection() successfully reads the values but once I try to actually use said values for a variable it only ever returns null. None of the solutions I found worked. Json file { …
Angel
  • 21
  • 3
1
vote
1 answer

Dynamically adding a Service with a Generic by an Activator?

Ok, the Title already sounds a bit silly, but I don't even know how to describe this better. So let's try to outline the problem. What I've figured out so far is the following, which is working: Given I have an interface like that public interface…
bquarta
  • 559
  • 6
  • 19
1
vote
0 answers

.NET Core - Apply Multiple Constructor Discovery Rules in Implementation Factory

I am working with c# and .NET Core 3.1, and I have a class that looks like this. public class MyClass : IMyClass { public MyClass() { … } public MyClass(IDependency dependency) { … } public…
1
vote
2 answers

.NET Core Dependency Injection for multilevel, multiple implementation dependencies

I have come across several posts where a single interface is implemented by multiple classes and the dependency is registered and resolved. One such post is this one. But how to resolve multiple, multi-level dependencies? For example: public enum…
1
vote
1 answer

Using ILogger without constructor injection

I have my registration and constructor setup to use logging via Microsoft.Extensions.Logging as follows, public MyService(ILogger logger) {} What I'd like to do, though, is use services.AddSingleton() in my Startup.cs to avoid using…
McArthey
  • 1,614
  • 30
  • 62
1
vote
1 answer

How to use DependencyResolver in Net48 selfhosted application?

I have gotten a task that contains creating a .Net 4.8 application that contains a "HttpSelfHostServer". I'm stuck in the quest of assigning "IServiceCollection services" to config.DependencyResolver (of type…
1
vote
1 answer

ServiceCollection IOptions<> won't bind properly

I am trying to bind some configurable settings. The values are provided via the app-settings.local.json. The Model to bind to is called Configurable Settings. I have attempted to follow a few tutorials and troubleshoot the…
shinyshark
  • 81
  • 8
1
vote
0 answers

Created a new ServiceCollection, added services, but GetService returns null

I've create a new servicecollection object and added a service. But, when I build the service provider and try to get service, it returns null. Example below. IServiceCollection services = new ServiceCollection(); …
Rob White
  • 950
  • 1
  • 6
  • 16
1
vote
1 answer

C# Service Collection adds type as object rather than its actual type

I'm having a little issue with my code. Basically, I'm trying to dynamically add classes that I've created from deserializing JSON into my ServiceCollection, so I can get them from any class that needs them. So far I've got the following…
1
vote
1 answer

What is ObjectPoolProvider and why does the asp.net core IServiceCollection have this injected by default

I have noticed that the ServiceCollection on setting up a new asp.net core application has Microsoft.Extensions.ObjectPool.ObjectPoolProvider injected by the framework by default. Why is this the case and more importantly when are we supposed to use…
Thulani Chivandikwa
  • 3,402
  • 30
  • 33
1
2