Blazor server. Scoped Service created twice outside the @Body

Марат Шакуров 1 Reputation point
2021-03-26T11:57:13.713+00:00

If Razor Component1 injecting Scoped Service1 is inserted outside the @Tomas Podoba of the LayoutComponent, then Service1 is created 2 times. Even if you insert 100 such components outside the @Tomas Podoba , two services will be created nevertheless. If Component1, or 100 such components is inserted inside another Razor Component2 inside the @Tomas Podoba , 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>  
,,,  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,404 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Wang-MSFT 1,051 Reputation points
    2021-03-29T08:32:46.14+00:00

    The Razor Pages or MVC portion of the app treats scoped services normally and recreates the services on each HTTP request when navigating among pages or views or from a page or view to a component.

    ASP.NET Core Blazor dependency injection

    You could try injecting Singleton service. Singleton DI creates a single instance of the service. All components requiring a Singleton service receive an instance of the same service.

    ------
    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    If the answer is helpful, please click "Accept Answer" and vote it up.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Michael Wang

    0 comments No comments

  2. Марат Шакуров 1 Reputation point
    2021-03-29T16:24:38.167+00:00

    Singleton service is single for all users, for all circuits... It is not solution.

    I understand that this problem is about pre-rendering.

    But, in many manuals and tutorials, Microsoft suggest putting, for example, the AuthorizeView in the App component, wrapping the RouteView. Next, the RouteView includes a LayoutComponentBase, and Microsoft suggest inserting any components and content into the LayoutComponentBase, including those that might inject a service prepared inside the AuthorizeView. But Microsoft are not warning that these components, which are outside the @Tomas Podoba fragment, will be re-created, and worst of all, injected scoped services will be re-created too.
    Interesting, It turns out that, on the one hand, Blazor Server offers a convenient markup with Many Pages, but as One SPA, i.e. Layout with Menu, Title and any Pages the router can detects. On the other hand, in fact, it turns out that all services injected in components/pages will be created twice if these components/pages are outside @Tomas Podoba fragment.

    Tell me:

    I just didn't understand the design approach of the "Blazor Server Main Layout" and I must not use DI outside @Tomas Podoba ?
    Instead, why in the tutorials/manuals Microsoft do not warn about such specific behavior of scoped services?
    Or is it just a bug?

    0 comments No comments

  3. Mayur Ekbote 1 Reputation point
    2021-07-05T13:23:19.097+00:00
    0 comments No comments