blazor server | Scoped singletons have two instances between .cshtml files and .razor files. ? RRS feed

  • Question

  • User2110873642 posted

    My main question

    so it appears that .razor files have a different instance of scoped singletons, than the rest of the website.

    can it be solved? or do i need to jump ship to browser SessionStorage?

    My use scenario

    i need to share scoped string, because i want to catch the user's IP adress, and pass it over to the .razor components trough the scoped singleton. as there is no good way to retrieve it in components. is there?

    i tried passing the IP to components, with the code below (failing cry)


    @inject BlazorApp.Services.SessionService SessionService
        if (Request.HttpContext.Connection.RemoteIpAddress != null)
            SessionService.UserIpAdress = Request.HttpContext.Connection.RemoteIpAddress.ToString();

    //the console returned a working IP adress, and not nothing :D Console.WriteLine(SessionService.UserIpAdress); }

    Scoped singleton class:

        public class SessionService
            public string UserIpAdress { get; set; }


    @page "/"
    @inject BlazorApp.Services.SessionService SessionService
    //The webpage shows no IP adress. When spying in RAM the field is 'null'.
    //Altough, im sure that the cshtml has stored the IP adress before the .razor components load

    <p> IP: @SessionService.UserIpAdress </p>

    i am aware that ip adresses can be spoofed. it does not matter for my use scenario

    Tuesday, August 4, 2020 3:44 AM

All replies

  • User475983607 posted

    There is no such thing as a scoped singleton.  

    i am aware that ip adresses can be spoofed. it does not matter for my use scenario

    In an Internet application the IP address is not the client IP address.  It's the IP address of the Client's WAN address.  This IP will only work if the clients and server are all on the same local network.

    Tuesday, August 4, 2020 10:35 AM