Blazor life time Scoped-service



  • I'm doing a spa on blazor and when View is closed and the Dispose method is challenged, it's not completely cleared, as the next transition to the same View does not require the design of the service. Service in a real project is declared as Scoped. Created as simple as possible. введите сюда описание изображения

    My problem.

    When you move between the deposits (First,Second) in TabControl, the signatures are removed and everything works well, but if I leave ProfileView on MainView, I need to release the services of ProfileService already, because I don't need it. He's getting free, but when he hits the next page of ProfileView, this service doesn't have a designer. As he doesn't have a designer, all subscriptions can't get their data, because in the Dispose method, I called OnCompleted at each field, which means I completed future updates.

    Question

    Why is it not for ProfileService designer to move to ProfileView if I called the Dispose method before?



  • I was answered by the only person https://stackoverflow.com/a/69842959/14119878 ♪ In general, there is no difference between scoped and singleton. In order to manage the life expectancy of the services, the component must be inherited from OwningComponentBase. If we trace this component, we clean up all the services that were received during work. Well, at first glance, it's fine, but not when I have the main window where there are ncons and each has a k window. If everyone traces the OwningComponentBase, n + k(possible) copies of this class are available that I don't fit.

    To date, two decisions have been found:

    1. Don't think there's some kind of service hanging out and leaving it as a scoped/singleton.
    2. The main component can be traced from OwningComponentBase, and all daughters from simple ComponentBase/ReactiveComponentBase. In the main ViewModel, we make public the services we need. After initializing the main form as parameters, we transmit these services to the subsidiary shape.

    Main VM

    public class MainViewModel : ViewModelBase
    {
        public IProfileService ProfileService { get; private set; }
    
    public MainViewModel(IProfileService profileService)
    {
        ProfileService = profileService;
    }
    

    }

    Main component MainView.razor.cs

    <h3>Main content...</h3>
    <FirstTabView ProfileService="ViewModel.ProfileService"/>

    FirstTabView.razor.cs

    public partial class FirstTabView
    {
    [Parameter]
    public IProfileService ProfileService { get; set; }

    protected override void OnParametersSet()
    {
        ViewModel = new FirstTabViewModel(ProfileService);
    }
    

    }

    There may be more compelling decisions, but at this point I don't know



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2