4

I'm trying to host my Blazor application on my server. I spent all the summer on it and I just realized every time I open my website on new device it doesn't create a new session restarting from zero, but continues where I left it. The worst part is there is a login system behind it, so I feel super dumb at the moment.

I really need a big hint on how to fix this "not little" issue. Is there a way to make server create new session every time someone open the website (without making it loose to other users)?

The solution should be use a Client Template instead, but the performance are really to slow.

UPDATE: Accounts "user password" are: - user user - test test

Download project sample (requires Net Core 3.0)

[SOLUTION] itminus found the solution to my issue.

You have also to add in ConfigureServices in Startup.cs this services.AddScoped<Storage>();

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddScoped<Storage>();
        }
3
  • Hi, this sounds like a solvable issue, but we need more information to solve it. Without being familiar at all with blazor, I would guess we'd need at a minimum to see some of the code-behind where you establish a session. Commented Sep 9, 2019 at 15:08
  • I added some little part of code Commented Sep 9, 2019 at 15:35
  • First Off, session isn't supported in SignalR apps... I'm not sure I understand your description. "on new device" means, what ? A different browser, say first time you access your app using FF, and then, Google Chrome ? Care to explain... how does your app create new session ? How do you know that "a new session" is "restarting from zero?" Commented Sep 9, 2019 at 20:35

1 Answer 1

5

every time I open my website on new device it doesn't create a new session restarting from zero, but continues where I left it.

I checkout your code and find that you're using Singleton Pattern to initialize the Storage. If I understand it correctly, this Storage singleton instance will be shared across different users (also across different devices). As this instance will be used to render the Main.razor page, there will be concurrency problems that you're experiencing now .

To fix that issue, the Storage instance should be limited within some specific connection. As you're using Blazor Server Side, you could register the Storage as a Scoped Service:

In Blazor Server apps, a scoped service registration is scoped to the connection. For this reason, using scoped services is preferred for services that should be scoped to the current user, even if the current intent is to run client-side in the browser.

Firstly, remove the static singleton instance :

public class Storage
{
    private static Storage instance;
    private Storage()                         
    {                                         
    }                                         
    public static Storage GetInstance()       
    {                                         
         if (Storage.instance == null)        
             Storage.instance = new Storage();
         return Storage.instance;             
     }                                        

    public List<Items>list {get;set;} = new List<Items>();
    public string password {get;set;}

}

Register this Class as a scoped service:

services.AddScoped<Storage>();

And then inject this service in your Login.razor and Main.razor :

@inject project.Storage Storage

Finally, you need change all the Storage.GetInstance(). to Storage.:

    Storage.list = Order;
    ...
    Storage.password = password;

I notice that you're also creating the Importer/Additional instance using the Singleton Pattern. I would suggest you should refactor them to use Service Injection in a similar way.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. This is the solution. There is also a little part where you have to add services.AddScoped<Storage>(); in Startup.cs. I wrote that in the main post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.