{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/aspnetcore/issues/18313","repository_url":"https://api.github.com/repos/dotnet/aspnetcore","labels_url":"https://api.github.com/repos/dotnet/aspnetcore/issues/18313/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/aspnetcore/issues/18313/comments","events_url":"https://api.github.com/repos/dotnet/aspnetcore/issues/18313/events","html_url":"https://github.com/dotnet/aspnetcore/issues/18313","id":548955544,"node_id":"MDU6SXNzdWU1NDg5NTU1NDQ=","number":18313,"title":"Blazor - Singleton service shares between instances in Server hosting model","user":{"login":"carlosld","id":1881381,"node_id":"MDQ6VXNlcjE4ODEzODE=","avatar_url":"https://avatars1.githubusercontent.com/u/1881381?v=4","gravatar_id":"","url":"https://api.github.com/users/carlosld","html_url":"https://github.com/carlosld","followers_url":"https://api.github.com/users/carlosld/followers","following_url":"https://api.github.com/users/carlosld/following{/other_user}","gists_url":"https://api.github.com/users/carlosld/gists{/gist_id}","starred_url":"https://api.github.com/users/carlosld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carlosld/subscriptions","organizations_url":"https://api.github.com/users/carlosld/orgs","repos_url":"https://api.github.com/users/carlosld/repos","events_url":"https://api.github.com/users/carlosld/events{/privacy}","received_events_url":"https://api.github.com/users/carlosld/received_events","type":"User","site_admin":false},"labels":[{"id":1164776385,"node_id":"MDU6TGFiZWwxMTY0Nzc2Mzg1","url":"https://api.github.com/repos/dotnet/aspnetcore/labels/area-blazor","name":"area-blazor","color":"5319e7","default":false,"description":"Includes: Blazor, Razor Components"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2020-01-13T14:11:01Z","updated_at":"2020-01-13T15:21:03Z","closed_at":null,"author_association":"NONE","body":"In .net core 3.1 a Singleton service instance is shared with all instances of a Server side Blazor App. In the default template of Blazor Server side App I create `State` class like this.\r\n\r\n```c#\r\npublic class State\r\n{\r\n    public int Counter { get; set; }\r\n}\r\n```\r\n\r\nThen I configure the service in `ConfigureService`\r\n\r\n```c#\r\npublic void ConfigureServices(IServiceCollection services)\r\n{\r\n    // .... \r\n    services.AddSingleton<State>();\r\n}\r\n```\r\n\r\nAnd finally I change the Counter component like this\r\n\r\n```html\r\n@page \"/counter\"\r\n\r\n@inject State State\r\n\r\n<h1>Counter</h1>\r\n\r\n<p>Current count: @State.Counter</p>\r\n\r\n<button class=\"btn btn-primary\" @onclick=\"IncrementCount\">Click me</button>\r\n\r\n@code {\r\n\r\n    private void IncrementCount()\r\n    {\r\n        State.Counter++;\r\n    }\r\n}\r\n```\r\nWhen you connect with different browser the counter is shared, witch I think is a bug.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/aspnetcore/issues/comments/573715318","html_url":"https://github.com/dotnet/aspnetcore/issues/18313#issuecomment-573715318","issue_url":"https://api.github.com/repos/dotnet/aspnetcore/issues/18313","id":573715318,"node_id":"MDEyOklzc3VlQ29tbWVudDU3MzcxNTMxOA==","user":{"login":"pranavkm","id":174281,"node_id":"MDQ6VXNlcjE3NDI4MQ==","avatar_url":"https://avatars2.githubusercontent.com/u/174281?v=4","gravatar_id":"","url":"https://api.github.com/users/pranavkm","html_url":"https://github.com/pranavkm","followers_url":"https://api.github.com/users/pranavkm/followers","following_url":"https://api.github.com/users/pranavkm/following{/other_user}","gists_url":"https://api.github.com/users/pranavkm/gists{/gist_id}","starred_url":"https://api.github.com/users/pranavkm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pranavkm/subscriptions","organizations_url":"https://api.github.com/users/pranavkm/orgs","repos_url":"https://api.github.com/users/pranavkm/repos","events_url":"https://api.github.com/users/pranavkm/events{/privacy}","received_events_url":"https://api.github.com/users/pranavkm/received_events","type":"User","site_admin":false},"created_at":"2020-01-13T15:21:03Z","updated_at":"2020-01-13T15:21:03Z","author_association":"CONTRIBUTOR","body":"Hi @carlosld, you'll need a scoped service - https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#add-services-to-an-app. Singleton services are singleton across the lifetime of the app \\ across multiple connections (just as they are with controllers). "}}
{"actor":{"display_login":"pranavkm"}}