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
) |
_host.CSHTML
@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; }
}
Index.Razor
@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