大风释放管理和不受管理的资源
我正在创建服务器端的大型应用程序,并希望正确发布托管和未管理的资源。我是大麻的初学者,不确定我所做的是否正确。请给我一些指示。谢谢
//这是我的城市模型服务
namespace MyApplication.Services
{
public class BasCityService : IBasCityService, IDisposable
{
private readonly IGenericRepository<BasCity> _cityRepository;
private readonly ISysExceptionLogService _exceptionLogService;
public BasCityService(IGenericRepository<BasCity> cityRepository,
ISysExceptionLogService exceptionLogService)
{
_cityRepository = cityRepository;
_exceptionLogService = exceptionLogService;
}
public async Task<BasCity> GetByIdAsync(int id)
{
try
{
return await _cityRepository.GetByIdAsync(id);
}
catch(Exception ex)
{
await _exceptionLogService?.CreateLogAsync(ex, null,
"BasCityService.GetByIdAsync()");
return null;
}
}
//Other
............
public void Dispose()
{
_cityRepository.Dispose(true); //In GenericRepository do _dbContext.Dispose();
_exceptionLogService.Dispose();
}
}
//这是城市详细信息剃须刀页面,
@page "/Base/City/CityDetails/{id:int}"
@inject IBasCityService cityService
@inject ISysExceptionLogService exceptionLogService
.........
@code {
protected override async Task OnInitializedAsync()
{
city = new BasCity();
try
{
city = await cityService?.GetByIdAsync(id);
if (city == null)
throw new NullReferenceException("Can't find City object by Id");
}
catch(Exception ex)
{
await processError?.LogError(Exception ex);
}
}
.............
//Dispose resource
// To detect redundant calls
private bool _disposedValue;
~CityDetails() => Dispose(false);
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
city = null;
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
cityService?.Dispose();
if (cityService != null) cityService = null;
exceptionLogService?.Dispose();
if (exceptionLogService != null) exceptionLogService = null;
_disposedValue = true;
}
}
}
请告诉我CityDetails页面是否更改为其他页面,例如CityList页面。 CityService&amp;最终器可以正确处理异常服务资源。另外,我们通常需要哪些托管资源来清理// todo:处置托管状态(托管对象)?非常感谢
I am creating a server side Blazor application and want to release managed and unmanaged resource correctly. I am beginner of Blazor and not sure if what I did is correct or not. Please give me some instructions. Thanks
//This is my service for city model
namespace MyApplication.Services
{
public class BasCityService : IBasCityService, IDisposable
{
private readonly IGenericRepository<BasCity> _cityRepository;
private readonly ISysExceptionLogService _exceptionLogService;
public BasCityService(IGenericRepository<BasCity> cityRepository,
ISysExceptionLogService exceptionLogService)
{
_cityRepository = cityRepository;
_exceptionLogService = exceptionLogService;
}
public async Task<BasCity> GetByIdAsync(int id)
{
try
{
return await _cityRepository.GetByIdAsync(id);
}
catch(Exception ex)
{
await _exceptionLogService?.CreateLogAsync(ex, null,
"BasCityService.GetByIdAsync()");
return null;
}
}
//Other
............
public void Dispose()
{
_cityRepository.Dispose(true); //In GenericRepository do _dbContext.Dispose();
_exceptionLogService.Dispose();
}
}
//This is city details razor page
@page "/Base/City/CityDetails/{id:int}"
@inject IBasCityService cityService
@inject ISysExceptionLogService exceptionLogService
.........
@code {
protected override async Task OnInitializedAsync()
{
city = new BasCity();
try
{
city = await cityService?.GetByIdAsync(id);
if (city == null)
throw new NullReferenceException("Can't find City object by Id");
}
catch(Exception ex)
{
await processError?.LogError(Exception ex);
}
}
.............
//Dispose resource
// To detect redundant calls
private bool _disposedValue;
~CityDetails() => Dispose(false);
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
city = null;
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
cityService?.Dispose();
if (cityService != null) cityService = null;
exceptionLogService?.Dispose();
if (exceptionLogService != null) exceptionLogService = null;
_disposedValue = true;
}
}
}
Please tell me if CityDetails page changes to other page such as CityList page. cityService & exceptionLogService resource can be disposed correctly by the finalizer. Also what managed resource we usually need to clean up inside // TODO: dispose managed state (managed objects)? Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论