Try our conversational search powered by Generative AI!

How to register service in the startup.cs file.

Vote:
 

Hi
I have implemented the one of setting services below

public class SettingsService : ISettingsService
{
}

and I did dependency injection like the below 

public void ConfigureServices(IServiceCollection services)
{
            services.AddSingleton<ISettingsService,SettingsService>();
            services.AddTransient<ISettingsService,SettingsService>();
}

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

#319943
Apr 03, 2024 17:32
Vote:
 

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).

#319983
Apr 03, 2024 23:40
Vote:
 

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 

#319985
Apr 04, 2024 7:02
Vote:
 

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
{
}
#319987
Apr 04, 2024 8:13
Vote:
 

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. 

#319995
Apr 04, 2024 13:21
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.