0

I am uncertain if the problem is on my side (my code) or if the serversetup is responsible for the unwanted behavier. I made a Asp.net single page project with the framework Blazor. It is simular to MVC Asp.net. In my Blazor project I setup a dependency injection service in the startup.cs file. And made its lifetime of singelton (one object instance per server start to "infinity" as far as I understand this).

In that service I store the responce of an Api call so that the data is right there in "memory forever". That is the theory (the expacted behavier). In reality my data is there alright. I checked/look it up from different IP adresses/PC/Mobile all good, the Api data is shown to the user. But the data is lost after a certain timeperiode instead of beeing there "forever" (we are talking it only takes a few minutes -> data lost). Could it be that my provider resets my webside/server because of inactivity? Or do I make a mistake in my code that causes that loss? I thought a singelton service would last as long as the service runs on the server. I try to provide the right code bits here:

Asp.net Core Blazor ServerSide Project:

file-> Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<ApiResponseService>();
    }

file-> My ApiResponseService Class

   public class ApiResponseService
{
    //--NowCast
    public List<ClimaCellNowCast> LstClimaCellNowCasts { get; set; }
    //create a HttpClient with the correct apiCallStringApikey etc
    public ApiCallCreateClient NowCastHttp { get; } = new ApiCallCreateClient(Const.NowCastApiKeyCallString);
    
    //...
    
    //--Update Methode
    public async Task<bool> UpdateWeatherData()
    {
    LstClimaCellNowCasts = await this.GetWeatherDataApiAsync<ClimaCellNowCast>(this.NowCastHttp);
        return updateOkay;
    }

    public async Task<List<T>> GetWeatherDataApiAsync<T>(ApiCallCreateClient clientT)
    {
        List<T> climaCell = new List<T>();

        using HttpResponseMessage response = await 
            clientT.Client.GetAsync(clientT.Client.BaseAddress);
            if (response.IsSuccessStatusCode)
            {
                clientT.MyApiJsonResponse = await response.Content.ReadAsStringAsync();
                // nugetPacket ->  Newtonsoft.Json
                climaCell = JsonConvert.DeserializeObject<List<T>>(clientT.MyApiJsonResponse);
             }
        return climaCell;
    }
}

file -> blazor component // the webside //

@inject ApiResponseService apiCall;
//...
@apiCall.doSomething //show the Api response data

//...
@code{
 protected override async void OnInitialized()
 {
     UpdateSuccessfull = await apiCall.UpdateWeatherData();
     this.StateHasChanged();
 }

 bool UpdateSuccessfull;

 private async Task GetWeatherData()
 {
    UpdateSuccessfull = await apiCall.UpdateWeatherData();
 }
}

Is there a way that my api response doesnt get "lost" after time? ^nogood

nogood
  • 1,117
  • 1
  • 11
  • 34
  • 1
    `Could it be that my provider resets my webside/server because of inactivity?` Yes. That is almost certainly what is happening. – mjwills Aug 24 '20 at 08:11
  • `Is there a way that my api response doesnt get "lost" after time?` Store it in a database (SQL Server, Couchbase, Redis etc). – mjwills Aug 24 '20 at 08:11
  • Could you please tell me if you run your app under webserver? If yes, which webserver? IIS? – Alireza Mahmoudi Aug 24 '20 at 08:13
  • How is `UpdateWeatherData` called? – mjwills Aug 24 '20 at 08:13
  • @mjwills thx! Okay so I should use a DB (I can use MongoDB for that). I thought that I could skip it because it is "only" one string of data to store. I did "read/write" it to a txt-file on the webserver! Is that a big no no or would that be okay for as said only 1 value (long string) to store. – nogood Aug 24 '20 at 10:04
  • the update is called from the blazor-webside-component apiCall=inject see the last code bit above @code{ protected override async void OnInitialized() { UpdateSuccessfull = await apiCall.UpdateWeatherData(); this.StateHasChanged(); } bool UpdateSuccessfull; private async Task GetWeatherData() { UpdateSuccessfull = await apiCall.UpdateWeatherData(); } } – nogood Aug 24 '20 at 10:04
  • @Alireza Mahmoudi Yes I think it is IIS (my webserver provider dashboard says: ASP.NET Web Hosting). – nogood Aug 24 '20 at 10:11
  • A text file _could_ work. Yes. – mjwills Aug 24 '20 at 11:02
  • My provider ansered my email:...The applicationpool idletime is set to 5 minutes by default. ... So that means my singelton life time is 5 Min. ;) atleast I now know what happens – nogood Aug 24 '20 at 14:25

1 Answers1

0

My provider ansered my email:...The applicationpool idletime is set to 5 minutes by default. ... So that means my singelton life time is 5 Min. ;) atleast I now know what happend

nogood
  • 1,117
  • 1
  • 11
  • 34