{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Docs/issues/8915","repository_url":"https://api.github.com/repos/aspnet/Docs","labels_url":"https://api.github.com/repos/aspnet/Docs/issues/8915/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Docs/issues/8915/comments","events_url":"https://api.github.com/repos/aspnet/Docs/issues/8915/events","html_url":"https://github.com/aspnet/Docs/issues/8915","id":367964693,"node_id":"MDU6SXNzdWUzNjc5NjQ2OTM=","number":8915,"title":"Can I send messages to clients from outside of SignalR hub?","user":{"login":"gaazkam","id":26407962,"node_id":"MDQ6VXNlcjI2NDA3OTYy","avatar_url":"https://avatars1.githubusercontent.com/u/26407962?v=4","gravatar_id":"","url":"https://api.github.com/users/gaazkam","html_url":"https://github.com/gaazkam","followers_url":"https://api.github.com/users/gaazkam/followers","following_url":"https://api.github.com/users/gaazkam/following{/other_user}","gists_url":"https://api.github.com/users/gaazkam/gists{/gist_id}","starred_url":"https://api.github.com/users/gaazkam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gaazkam/subscriptions","organizations_url":"https://api.github.com/users/gaazkam/orgs","repos_url":"https://api.github.com/users/gaazkam/repos","events_url":"https://api.github.com/users/gaazkam/events{/privacy}","received_events_url":"https://api.github.com/users/gaazkam/received_events","type":"User","site_admin":false},"labels":[{"id":327109981,"node_id":"MDU6TGFiZWwzMjcxMDk5ODE=","url":"https://api.github.com/repos/aspnet/Docs/labels/P1","name":"P1","color":"fbca04","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/aspnet/Docs/milestones/4","html_url":"https://github.com/aspnet/Docs/milestone/4","labels_url":"https://api.github.com/repos/aspnet/Docs/milestones/4/labels","id":1356168,"node_id":"MDk6TWlsZXN0b25lMTM1NjE2OA==","number":4,"title":"Backlog","description":"Issues that have been triaged but aren’t yet scheduled. Issues not assigned to any milestone are those that have not been triaged.","creator":{"login":"danroth27","id":1874516,"node_id":"MDQ6VXNlcjE4NzQ1MTY=","avatar_url":"https://avatars2.githubusercontent.com/u/1874516?v=4","gravatar_id":"","url":"https://api.github.com/users/danroth27","html_url":"https://github.com/danroth27","followers_url":"https://api.github.com/users/danroth27/followers","following_url":"https://api.github.com/users/danroth27/following{/other_user}","gists_url":"https://api.github.com/users/danroth27/gists{/gist_id}","starred_url":"https://api.github.com/users/danroth27/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danroth27/subscriptions","organizations_url":"https://api.github.com/users/danroth27/orgs","repos_url":"https://api.github.com/users/danroth27/repos","events_url":"https://api.github.com/users/danroth27/events{/privacy}","received_events_url":"https://api.github.com/users/danroth27/received_events","type":"User","site_admin":false},"open_issues":535,"closed_issues":523,"state":"open","created_at":"2015-10-14T20:43:26Z","updated_at":"2018-10-18T04:43:49Z","due_on":null,"closed_at":null},"comments":0,"created_at":"2018-10-08T21:48:26Z","updated_at":"2018-10-18T04:55:22Z","closed_at":null,"author_association":"NONE","body":"Terribly sorry for persisting on this issue... But I do believe that the docs would benefit from mentioning this in a more explicit way.\n\nI have come to a solution which seems to be working, but Im not sure if it is the correct one.\n\nI need my hub to send a message to the clients... not immediatelly though, but - lets say - 2 mins after the appropriate method on the hub is invoked. This is my solution:\n\n    public class ChatHub : Hub\n    {\n        private readonly IServiceScopeFactory scopeFactory;\n        public ChatHub(IServiceScopeFactory scopeFactory)\n        {\n            this.scopeFactory = scopeFactory;\n        }\n\n        public async Task DelayedResponse(string message)\n        {\n            var timer = new Timer () { Interval = 2 * 60 * 1000, AutoReset = false };\n            timer.Elapsed += async (sender, e) =&gt;\n            {\n                using (var scope = scopeFactory.CreateScope())\n                {\n                    var Clients = scope.ServiceProvider.GetRequiredService&lt;IHubContext&lt;ChatHub&gt;&gt;().Clients;\n                    await Clients.All.SendAsync(\"ReceiveChatMessage\", message);\n                }\n            }\n        }\n    }\n\nThis seems to work however, I'm not sure if this is the correct solution, or if this only works by coincidence. My doubts are based on the fact that:\n\n1. I'm using `IServiceScopeFactory` in this manner only because I read on StackOverflow that this is a Singleton ( https://stackoverflow.com/a/39126718/4385532 ) However, I could not find such an info in the documentation ( https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.iservicescopefactory?view=aspnetcore-2.1 ) which is why I'm unsure if this usage is correct. All I know is that it doesnt seem to be Disposed by the time the timer's handler fires off so it seems to be working.\n2. Am I supposed to instantiate a HubContext? I suppose yes, but again I cant find this explicitely mentioned in the documentation.\n\nAgain - while I did come to a solution that seems to be working, I do believe that the doc pages could benefit from explicit answers to these questions.\n\nThese considerations are needed because the straightforward approach fails to work. The below code throws because Clients are disposed:\n\n    public class ChatHub : Hub\n    {\n        public async Task DelayedResponse(string message)\n        {\n            var timer = new Timer () { Interval = 2 * 60 * 1000, AutoReset = false };\n            timer.Elapsed += async (sender, e) =&gt;\n            {\n                    await Clients.All.SendAsync(\"ReceiveChatMessage\", message);\n            }\n        }\n    }\n\n\n---\n#### Document Details\n\n⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*\n\n* ID: 0d78f540-81fd-1ba9-cb90-591e4ec27d9b\n* Version Independent ID: 2251f557-dd3a-ec35-2558-6243337db009\n* Content: [Dependency injection in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1#call-services-from-main)\n* Content Source: [aspnetcore/fundamentals/dependency-injection.md](https://github.com/aspnet/Docs/blob/master/aspnetcore/fundamentals/dependency-injection.md)\n* Product: **aspnet-core**\n* GitHub Login: @guardrex\n* Microsoft Alias: **riande**"},"comment":{"url":"https://api.github.com/repos/aspnet/Docs/issues/comments/430875007","html_url":"https://github.com/aspnet/Docs/issues/8915#issuecomment-430875007","issue_url":"https://api.github.com/repos/aspnet/Docs/issues/8915","id":430875007,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMDg3NTAwNw==","user":{"login":"Rick-Anderson","id":3605364,"node_id":"MDQ6VXNlcjM2MDUzNjQ=","avatar_url":"https://avatars1.githubusercontent.com/u/3605364?v=4","gravatar_id":"","url":"https://api.github.com/users/Rick-Anderson","html_url":"https://github.com/Rick-Anderson","followers_url":"https://api.github.com/users/Rick-Anderson/followers","following_url":"https://api.github.com/users/Rick-Anderson/following{/other_user}","gists_url":"https://api.github.com/users/Rick-Anderson/gists{/gist_id}","starred_url":"https://api.github.com/users/Rick-Anderson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Rick-Anderson/subscriptions","organizations_url":"https://api.github.com/users/Rick-Anderson/orgs","repos_url":"https://api.github.com/users/Rick-Anderson/repos","events_url":"https://api.github.com/users/Rick-Anderson/events{/privacy}","received_events_url":"https://api.github.com/users/Rick-Anderson/received_events","type":"User","site_admin":false},"created_at":"2018-10-18T04:55:22Z","updated_at":"2018-10-18T04:55:22Z","author_association":"CONTRIBUTOR","body":"@anurse \tplease respond"}}
{"actor":{"display_login":"Rick-Anderson"}}