title: Blazor server Javascript Websocket callback to c# injected service does not work
body: <p>I have a blazor server app, that calls an injected service to call a web socket in javascript to get some data.  I want the web socket to return the data to a c# instance callback on the service. The c# callback works fine from the javascript method, but not from the any of the websocket callbacks (e.g. onmessge, onerror, onclose, etc.).  No errors, just no data.</p>
  <p>I have tried making the instance a global javascript and even a static value in the calling class, but still does not work.  I have googled to no end and tried every suggestion, but still no luck.</p>
  <p>Here is index page:</p>
  <pre><code>@page &quot;/&quot;
  @inject BlazorJavaScriptCallback.IService1 _service1
  
  &lt;h1&gt;Hello, world!&lt;/h1&gt;
  
  &lt;p&gt;And the answer back from the web service is @Message&lt;/p&gt;
  
  @code {
  
      protected string Message  {get; set;}
  
      protected override async Task OnAfterRenderAsync(bool firstRender)
      {
          if (firstRender)
          {
              Message = await _service1.CallWebService();
              StateHasChanged();
          }
      }
  }
  </code></pre>
  <p>The service:</p>
  <pre><code>using Microsoft.JSInterop;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace BlazorJavaScriptCallback
  {
      public interface IService1
      {
          Task&lt;string&gt; CallWebService();
      }
  
  
      public class Service1 : IService1
      {
  
          AutoResetEvent _stopWaitHandle = new AutoResetEvent(false);
          String _message;
  
          private IJSRuntime _jsruntime;
  
          public Service1(IJSRuntime jsruntime)
          {
              _jsruntime = jsruntime;
          }
  
          public async Task&lt;string&gt; CallWebService()
          {
              _message = string.Empty;
              var dotNetObjRef = DotNetObjectReference.Create(this);
              await _jsruntime.InvokeVoidAsync(&quot;callWebService&quot;, dotNetObjRef, &quot;OutputMessage&quot;);
              _stopWaitHandle.WaitOne();
  
              return _message;
          }
  
          [JSInvokable]
          public async Task OutputMessage(string message)
          {
              _message = message;
              Console.WriteLine($&quot;**** OutputMessage {message}&quot;);
              _stopWaitHandle.Set();
          }
  
      }
  }
  </code></pre>
  <p>The javascript:</p>
  <pre><code>async function callWebService(instance, method) {
  
      instance.invokeMethodAsync(method, &quot;start&quot;);
  
      var wsUri = &quot;wss://127.0.0.1:80/Test/&quot;;
      var websocket = new WebSocket(wsUri);
      console.log(&quot;websocket has been setup&quot;);
  
      websocket.onopen = function (e) {
          console.log(&quot;websocket connected&quot;);  
          websocket.send(&quot;Hellow world&quot;)
      };
  
      websocket.onclose = function (e) {
          instance.invokeMethodAsync(method, &quot;closed&quot;);
          console.log(&quot;websocket closed&quot;);
      };
  
      websocket.onmessage = function (e) {
          console.log(&quot;websocket got message&quot; + e.data);
          instance.invokeMethodAsync(method, e.data);
          websocket.close();
      };
  
      websocket.onerror = function (e) {
          console.log(&quot;websocket error&quot;);
          instance.invokeMethodAsync(method, &quot;error&quot;);
          websocket.close();
      };
  
  }
  </code></pre>
  <p>You will need to add this to the startup in ConfigureServices:</p>
  <pre><code>services.AddScoped&lt;IService1, Service1&gt;();
  </code></pre>
  <p>You will need to add the following to your _Hosts.cshtml:</p>
  <pre><code>&lt;script src=&quot;~/scripts/Javascript.js&quot;&gt;&lt;/script&gt;
  </code></pre>
  <p>EDIT: it works when I try this without an injected service (putting the call and callback right in the razor page).  Hrmmm.</p>