question

96112964 avatar image
0 Votes"
96112964 asked MayurEkbote-2725 published

Blazor server. Scoped Service created twice outside the @Body

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>
 ,,,


dotnet-aspnet-core-generaldotnet-aspnet-core-blazor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

miwan2-msft avatar image
0 Votes"
miwan2-msft answered

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




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

96112964 avatar image
0 Votes"
96112964 answered

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 @Body 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 @Body fragment.

Tell me:

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MayurEkbote-2725 avatar image
0 Votes"
MayurEkbote-2725 answered MayurEkbote-2725 published
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.