title: Dependency Injection on SignalR in ASP.NET Core
body: <p><strong>Note:</strong> </p>
  
  <p>For brevity, I reduced my codes and made it simple. </p>
  
  <p><strong>Codes/Setup:</strong></p>
  
  <p>I have a class:</p>
  
  <pre><code>class MyService
  {
      public Guid InstanceID = Guid.NewGuid;
  </code></pre>
  
  <p>I added <code>MyService</code> in my services as scoped:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
     services.AddScoped&lt;MyService, MyService&gt;();
  </code></pre>
  
  <p>Then in my view, I inject the service:</p>
  
  <pre><code>@inject MyService _myService
  
  @_myService.InstanceID //display
  </code></pre>
  
  <p>The code above displays different result each request, as expected from a scoped service.</p>
  
  <p>However, when injecting the service in SignalR Hub, I always get the same output.</p>
  
  <pre><code>MyHub(MyService myService)
  {
      _myService = myService; 
  }
  
  public void Test()
  {
       Clients.Caller.Log(_myService.InstanceID); //Log is a custom function in JS
  </code></pre>
  
  <p>Although the Hub itself is instantiated every call to <code>Test()</code>, the instance of <code>MyService</code> being injected to it is always the same.</p>
  
  <p><strong>My Requirement:</strong></p>
  
  <p>I want a new instance of <code>MyService</code> to be injected in Hub the same way in Controller/View every request.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Is this the default behavior of SignalR? Or am I doing something wrong?</p>
  
  <p><strong>Packages Used:</strong> </p>
  
  <pre><code>&lt;PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /&gt;
  
  &lt;PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="0.2.0-*" /&gt;
  &lt;PackageReference Include="Microsoft.AspNetCore.SignalR.Server" Version="0.2.0-rtm-22752" /&gt;
  </code></pre>