If Razor Component1 injecting Scoped Service1 is inserted outside the @Body of the LayoutComponent, then Service1 is created 2 times. Even if you insert 100 such components outside the @Body, two services will be created nevertheless. If Component1, or 100 such components is inserted inside another Razor Component2 inside the @Body, then Service1 is created once. What is the problem and how to work around it if I need to display Service1's data on the main View without having two instntiations of the Service1?
Sample MainLayout.razor:
@* MainLayout.razor *@
...
@inherits LayoutComponentBase
...
<div class="main">
<div class="top-row px-4">
<div>
<Component1InjectingService1 />
<Component2InjectingService1 />
...
<Component100InjectingService1 />
</div>
</div>
</div>
...
<div class="content px-4">
@Body
</div>
Another sample with single Scoped Service1 instance, MainLayout.razor, PageWithComponent1InjectingService1.razor, Component1InjectingService1.razor:
@* ----------------- *@
@* MainLayout.razor *@
...
@inherits LayoutComponentBase
...
<div class="main">
<div class="top-row px-4">
<div>
weird things
</div>
</div>
</div>
...
<div class="content px-4">
@Body
</div>
@* ------------------------------------------ *@
@* PageWithComponent1InjectingService1.razor *@
@page "pagewithcomponent1injectingservice1"
...
<Component1InjectingService1 />
...
@* ---------------------------------- *@
@* Component1InjectingService1.razor *@
...
@inject Service1 service1
...
<div>
@service1.Info
</div>
,,,