Hi Kumar
Seems you created and registered the service. Did you also use it in a constructor somewhere? If not, then the service will never be instantiated. Even when it is a singleton, it is not instantiated until the first service request.
Besides, you should settle for one lifecycle (of singleton, transient or scoped).
You should only register your implementation once
in your case the second line will override the first
what do you mean "not calling". these lines are simply registering to the DI that, when an instance of ISettingsService is requested, create and return an instance of SettingsService. it does not "Call" SettingsService until you actually use your ISettingsService
Hi Kumar,
My guess that you're trying to use the service as following:
public class Usage
{
private readonly SettingsService _settingsService;
public Usage(SettingsService settingsService)
{
_settingsService = settingsService;
}
}
In that case you'd need either:
a) Use the ISettingsService
in constructor
public class Usage
{
private readonly ISettingsService _settingsService;
public Usage(ISettingsService settingsService)
{
_settingsService = settingsService;
}
}
b) Register the concrete implementation
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<SettingsService>();
}
c) Utilize ServiceConfigurationAttribute
to register the service - it will automatically register both interface and concrete implementation to container
[EPiServer.ServiceLocation.ServiceConfiguration(typeof(ISettingsService))]
public class SettingsService : ISettingsService
{
}
b) is actually redundant - each class is registered as a Transient of itself. You only need to register it if 1. You register it as an implementation of an interface or a base class. 2. You want to change the default lifecycle (to Singleton for example)
I would recommend to keep the registration consistent, either in the ConfigureServices, or with the attribute. It seems for most developer, using ConfigureServices is preferred as you can have all registrations in one place.
Hi
I have implemented the one of setting services below
and I did dependency injection like the below
but it's not calling the SettingsService.
Could you please suggest to us how can we register the service in the Optimizely CMS 12 and .netcore 6.0