Search code examples
c#asp.netdependency-injectionblazor-server-sideobject-lifetime

How to setup : services.AddSingleton<ApiResponseService>(); with a lifetime of "server start -- till "infinity"" in a Asp.net project


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


Solution

  • 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