Topshelf windows service, Structure map - > achieve per request object lifecycle for timer elapsed event

729 views Asked by At

I have a windows service application hosted with Topshelf. I am initializing structure map container once, when the service is started. I have a timer, scheduled tasks which are triggered on each timer elapsed event. I would like to have something like "per request " object creation to have same instances of initialized objects just durring one timer tick, and to dispose them when the job is finished. On next tick of the timer I would like to have new object instances (e.g. objects like dbcontext for EF.. ). How can I achieve this with Structure map is it possible?

1

There are 1 answers

1
Matt Mills On BEST ANSWER

I think what you're looking for is the nested container:

http://structuremap.github.io/the-container/nested-containers/

The naive implementation would be to pass the application's container into the Elapsed event handler, call GetNestedContainer() on it, and then Dispose of the nested container before the handler exits.