1

I am working on a console application where I want to get the instance of a generic service type. Here is my implementation it gives me null.

 public class HelperService
        {
            private readonly ServiceCollection collection;
            private readonly IServiceProvider serviceProvider;
            public HelperService()
            {
                collection = new ServiceCollection();
                serviceProvider = collection.BuildServiceProvider();
            }
            public void RegisterService()
            {
                #region [register Services]
    
                collection.AddScoped<ICustomerService, CustomerService>();
    
                #endregion
    
            }
    
            public T? GetServiceInstance<T>() where T : class
            {
                return serviceProvider.GetService<T>()?? null;
            }
    
        }

var helperService = new HelperService();
helperService.RegisterService();
var result = helperService.GetServiceInstance<ICustomerService>(); // result is null

I want to implement generic service to which I will pass any service and it will give instance.

1 Answer 1

2

You are adding service to collection after the IServiceProvider was build so it will not know anything about this newly added service, you need to add service before building the provider:

    public class HelperService
    {
        private readonly ServiceCollection collection;
        private readonly IServiceProvider serviceProvider;
        public HelperService()
        {
            collection = new ServiceCollection();
            #region [register Services]

            collection.AddScoped<ICustomerService, CustomerService>();

            #endregion
            serviceProvider = collection.BuildServiceProvider();
        }

        public T? GetServiceInstance<T>() where T : class
        {
            return serviceProvider.GetService<T>();
        }
    }

Also ?? null does not make much sense and can be removed.

And in general I would say that such wrapper is not very helpful, at least based on provided code.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you please let me how to register IConfiguration I am facing an issue registering it
@MuhammadKamran try building configuration as it done for example here and than adding it to DI. This can be helpful.
I don't have appsetting.json file it is a console application
@MuhammadKamran you can add it. Or add any other source of configuration (like environment variables and so on) with Add... call. What is the source of config for your app?
I have solve the configuration but now I am facing this error Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.