title: SignalR: The 'SendCoreAsync' method cannot be called if the connection is not active
body: <p>I'm working on an ASP.NET Core web application in which users through the client (OrderClient) will be able to send and receive messages using a SignalR hub (OrderHub). I'm new to razor pages, so I am probably missing something obvious.</p>
  <p>For simplicity's sake, the UserConnected-method in the OrderHub looks like this:</p>
  <pre><code>    public async Task UserConnected(string someString)
      {
          log.Info(someString);
      }
  </code></pre>
  <p>My <code>index.cshtml.cs</code> looks like this:</p>
  <pre><code>public class IndexModel : PageModel
  {
      private readonly ILogger&lt;IndexModel&gt; _logger;
      private HubConnection _hubConnection;
  
      [BindProperty]
      public string Test { get; set; }
  
      public IndexModel(ILogger&lt;IndexModel&gt; logger)
      {
          _logger = logger;
          _hubConnection = new HubConnectionBuilder().WithUrl(&quot;https://localhost:44336/orderhub&quot;).WithAutomaticReconnect().Build();
      }
  
      public async void OnGet()
      {
          await _hubConnection.StartAsync();
      }
  
      public async Task&lt;IActionResult&gt; OnPostAsync()
      {
          if (!ModelState.IsValid)
          {
              return Page();
          }
          //await _hubConnection.StartAsync();
  
          await _hubConnection.SendAsync(&quot;UserConnected&quot;, Test);
  
          return RedirectToPage(&quot;./Index&quot;);
      }
  }
  </code></pre>
  <p>And my <code>index.cshtml</code> looks like this:</p>
  <pre><code>@page
  @model IndexModel
  @{
      ViewData[&quot;Title&quot;] = &quot;Home page&quot;;
  }
  
  &lt;form method=&quot;post&quot;&gt;
      Name:
      &lt;input asp-for=&quot;Test&quot; /&gt;
      &lt;input type=&quot;submit&quot; /&gt;
  &lt;/form&gt;
  </code></pre>
  <p>I've tried debugging in the OrderHub, and the connection is established correctly when the IndexModel-constructor is invoked along with the OnGet()-method. However, when I click the button on the page, the connection has been lost and I am shown the error &quot;InvalidOperationException: The 'SendCoreAsync' method cannot be called if the connection is not active&quot;. The state of the hub, when debugging, also shows as &quot;Disconnected&quot;. I am assuming I am handling the lifetime of the hub incorrectly, however I fail to see how to fix it.</p>
  <p>If I recreate the connection each time, the code works and the text from the input field is received correctly by the hub. However, since the connection is closed when not reconnecting each time, I assume the hub won't be able to send information to the client, which is the issue I foresee having. This could also be wrong, and that the client is able to receive messages - I haven't tried this yet.</p>