# Singleton service instantiated twice

**URL:** <https://forum.litium.com/t/singleton-service-instantiated-twice/1092>\
**Category:** Questions\
**Tags:** litium-7\
**Created:** [September 11, 2019, 2:50pm UTC](https://forum.litium.com/t/singleton-service-instantiated-twice/1092 "2019-09-11T14:50:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emilsjog](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@emilsjog](https://forum.litium.com/u/emilsjog)\
**Post date:** [September 11, 2019, 2:50pm UTC](https://forum.litium.com/t/singleton-service-instantiated-twice/1092/1 "2019-09-11T14:50:21Z")

</div>

I have a service and it’s service interface is registred as a singleton with the service decorator attribute.  
`[Service(ServiceType = typeof(IMySendService), Lifetime = DependencyLifetime.Singleton)]`  
The service is also implementing the `IStartupTask` because there is event handling in the service that needs to be registered at startup.  
So far so good…

But the service is also used in a scheduled task and injected with DI to the task. And if I debug and set a breakpoint in the constructor of the service it’s beeing hit twice.

Shouldn’t there only be one instance of my service with that attribute?

Litium version: 7.2.3

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [September 12, 2019, 4:32am UTC](https://forum.litium.com/t/singleton-service-instantiated-twice/1092/2 "2019-09-12T04:32:00Z")

</div>

The scheduled tasks that using the `Litium.Foundation.Tasks.ITask` interface is always created as transients. To work around and only create one instance of the service you need to wrap the service in an class that will be used as the task instance.

```auto
    public class MyTaskRunner : ITask, IStartupTask
    {
        private readonly MyInjectedService _service;

        public MyTaskRunner(MyInjectedService service)
        {
            _service = service;
        }

        public void ExecuteTask(SecurityToken token, string parameters)
        {
            _service.Execute();
        }

        public void Start()
        {
            _service.Execute();
        }
    }

    [Service(ServiceType = typeof(MyInjectedService), Lifetime = DependencyLifetime.Singleton)]
    public class MyInjectedService
    {
        public void Execute()
        {
            // do some stuff
        }
    }

```

Also the `IStartupTask` is in the older part of the code and will automatic register itself as singleton in the DI container without looking at the service-attribute.

---

<div class="post-metadata">

**Author:** ![emilsjog](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@emilsjog](https://forum.litium.com/u/emilsjog)\
**Post date:** [September 12, 2019, 6:35am UTC](https://forum.litium.com/t/singleton-service-instantiated-twice/1092/3 "2019-09-12T06:35:26Z")

</div>

Could you give an example on how the service should be wrapped in an class that will be used as the task instance? I did try to make a separate service that is used by the task and it’s injecting `IMySendService`. But didn’t got it working, still got multiple instances.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [September 12, 2019, 7:18am UTC](https://forum.litium.com/t/singleton-service-instantiated-twice/1092/4 "2019-09-12T07:18:35Z")

</div>

Se above example

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [October 10, 2019, 7:18am UTC](https://forum.litium.com/t/singleton-service-instantiated-twice/1092/5 "2019-10-10T07:18:42Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
