问题描述
我不确定问题是否出在我这一边(我的代码),或者服务器设置是否引起了不良行为。 我使用Blazor框架制作了一个Asp.net单页项目。与MVC Asp.net类似。在我的Blazor项目中,我在startup.cs文件中设置了依赖项注入服务。并使其寿命达到了辛格尔顿的状态(据我了解,每个服务器的一个对象实例开始达到“无限”状态。)
在该服务中,我存储了Api调用的响应,以便数据永远存储在“永远的内存”中。 这就是理论(确切的行为)。 实际上,我的数据还可以。我从不同的IP地址/ PC / Mobile进行了检查/查找,一切都很好,向用户显示了Api数据。但是数据在一定时间后会丢失,而不是“永远”呆在那里(我们说的只是几分钟->数据丢失)。可能是我的提供者由于不活动而重置了我的Webside /服务器?还是我在代码中犯了一个导致损失的错误?我认为只要服务在服务器上运行,singelton服务就可以持续下去。 我在这里尝试提供正确的代码位:
Asp.net Core Blazor ServerSide项目:
文件-> Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<ApiResponseService>();
}
file->我的ApiResponseService类
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组件//网络端//
@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();
}
}
有没有办法让我的api响应在一段时间后不会“丢失”? ^ nogood
解决方法
我的提供者回答了我的电子邮件:...默认情况下,applicationpool空闲时间设置为5分钟。 ...这意味着我的singelton寿命为5分钟。 ;)至少我现在知道发生了什么