Home > other >  How can I restore my C#variables that are binded to the resp. ASP.NET razor page, after I leave the
How can I restore my C#variables that are binded to the resp. ASP.NET razor page, after I leave the

Time:06-25

Hello I have following razor page in my Blazor project that is showing the eventlog data from a remote machine. The eventlog data is read in the respective .cs file with a C# code. The C# arrays are visualised then in the table rows. If a press the read button i get the eventlog data from the remote PC without problem. If I navigate then to another page and navigate back to my eventlog page again i get an empty table. That means the eventlog data (C# arrays) have been lost. I want, that the last read eventlog data is still available on the page, without reading it from the machine again. The reason is that the reading from remote PC takes a little bit long due to WMI (15-20 seconds). If the client needs each time fresh data of course he should press read button again, but if the last read data is OK for him,I want to offer him this data. How is this possible?

@page "/wmi_event"

<head>   
</head>
<h1>EventLog (Win Application Errors)</h1>

<body>

<button  @onclick="Read_EventLog">Read</button>
<p>Raed Time= @Read_time</p>

<table>

    <tr>
        <td>Log Time</td>
        <td>Log Source</td>
        <td>Log Message</td>
    </tr>
    @{
        for (var i = 0; i < 999; i  )
        {
        <tr>
            <td>@Eventlog_time[i]</td>
            <td>@Eventlog_source[i]</td>
            <td>@Eventlog_message[i]</td>
        </tr>
        }

    }

</table>
</body>

CodePudding user response:

You could put the event log into a scoped service as a property which you then inject and reference on the page.

The below has a guide on this for each type of Blazor app

https://docs.microsoft.com/en-us/aspnet/core/blazor/state-management?view=aspnetcore-5.0&pivots=webassembly#in-memory-state-container-service-wasm

CodePudding user response:

At the end I have learned that i can do this in the best way with SignalR in ASP.net core. Additionaly I saw that Blazor has in default this SignalR machanism. So I switched to Blazor and it was realy very easy. I suggest everyone who is not developing deep applications with Blazor for such kind of issues.

  • Related