Jump to content

Is IService a singleton instance?

Recommended Posts

My plugin implements IService.

Inside this service is a constructor which handles implementing a response client.

The ResponseClient  only needs one instance throughout the life of the interaction of the emby endpoint.


    public class RequestService : IService
        public RequestService(IJsonSerializer json, IHttpClient client)
            JsonSerializer = json;
            UserManager    = user;

            if (ResponseClient.Instance is null)
                Activator.CreateInstance(typeof(ResponseClient), json, client);


This is an example of the ResponseClient constructor

    public class ResponseClient : IResponseClient
        private IJsonSerializer JsonSerializer { get; }
        private IHttpClient HttpClient         { get; }
        public static IResponseClient Instance { get; set; }

        public ResponseClient(IJsonSerializer jsonSerializer, IHttpClient client)
            JsonSerializer = jsonSerializer;
            HttpClient     = client;
            Instance = this;


The "ResponseClient" is then accessed globally by the static "Instance" variable.


It did implement the IServerEntryPoint, however the server was holding on to the instance.


Because the ResponseClient instance is  created inside the IService instance, and use a static Instance variable, is it ever dispose or get garbage collected after use?

or does it persist because it is created in the IService, and is a singleton itself?

I placed a condition in the Service constructor which looks to see if it is null just in case it wasn't disposed of.


Does the IService instance only live for as long as it is needed during the incoming requests, or is the instance of every IService live for the lifetime of the server?

Just want to make my code as light weight as possible. I'm beginning to think that a singleton is not a way to do this.

Edited by chef
Link to post
Share on other sites

Excellent, then I will use IDisposable to get rid of the Response client object.


Many thanks.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...