title: Initialize Service in Blazor Webassembly
body: <p>I've got a service A that is shared among various components. That service needs to be initialized at startup with values from a WebApi.</p>
  <p>How can I accomplish that? I tried two things.</p>
  <p>_1.</p>
  <p>In program.cs/main method I do</p>
  <pre><code>builder.Services.AddScoped
  </code></pre>
  <p>with A and other services B and C.</p>
  <p>Then I call a method</p>
  <pre><code>async LoadSetupData(wasmBuilder)
  </code></pre>
  <p>which gets services A, B, C via</p>
  <pre><code>wasmBuilder.GetRequiredService&lt;T(A)&gt;...
  </code></pre>
  <p>and then</p>
  <pre><code>A.MySetupValues = await B.GetSetupValues().
  </code></pre>
  <p>The values are retrieved through the WebApi.</p>
  <p>That works so far. But when I use A in a component via [Inject] A a, the service object I get is not the one I retrieved in LoadSetupData (service has a Guid property, they are different).</p>
  <p>_2.</p>
  <p>I tried adding A by providing a factory method to AddScoped. That doesn't work because getting Values from a WebApi is intrinsically asynchronous. And asynchronous calls can't be used from program.cs/main (&quot;Cannot wait on monitors on this runtime&quot;).</p>
  <p>Thank you very much for reading! Any help is highly appreciated!</p>