{"id":805,"date":"2025-07-01T20:52:50","date_gmt":"2025-07-01T20:52:50","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=805"},"modified":"2025-07-14T19:26:25","modified_gmt":"2025-07-14T19:26:25","slug":"transient-services","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/advanced\/transient-services\/","title":{"rendered":"Per-Client Services"},"content":{"rendered":"<p>Sometimes you might need to inject a different instance of the same service to every client that requests the service. This can be achieved in Init(args) using <a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/value-providers\/\">value providers<\/a>:<\/p>\n<ol>\n<li>Create a class that implements <em>IValueProvider&lt;T&gt;<\/em>, where <em>T<\/em> is the concrete or defining type of your service.<\/li>\n<li>Implement <em>IValueProvider&lt;T&gt;.Value <\/em>in such a way, that a new instance of T is returned every time that the property is called.<\/li>\n<li>Add the <a href=\"https:\/\/docs.sisus.co\/init-args\/services\/service-attribute\/\">[Service] attribute<\/a> to the value provider class, and specify <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-defining-type\/\">defining types<\/a> for the service which are assignable from T<em>.<\/em><\/li>\n<\/ol>\n<pre>[Service(typeof(ILogger))]\r\nclass LoggerProvider : IValueProvider&lt;Logger&gt;\r\n{\r\n    public Logger Value = new Logger();\r\n}<\/pre>\n<p>You can also return services that are customized for the client making the request by providing a custom implementation for the <em>IValueProvider&lt;T&gt;.TryGetFor(Component client, out T value)<\/em> method:<\/p>\n<pre>[Service(typeof(ILogger))]\r\nclass LoggerProvider : IValueProvider&lt;Logger&gt;\r\n{\r\n    public Logger Value = new Logger(context: null);\r\n\r\n    public bool TryGetFor(Component client, out Logger logger)\r\n    {\r\n        logger = new Logger(context: client);\r\n        return true;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you might need to inject a different instance of the same service to every client that requests the service. This can be achieved in Init(args) using value providers: Create a class that implements IValueProvider&lt;T&gt;, where T is the concrete or defining type of your service. Implement IValueProvider&lt;T&gt;.Value in such a way, that a new ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/advanced\/transient-services\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/805"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=805"}],"version-history":[{"count":2,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/805\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/805\/revisions\/810"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}