大风释放管理和不受管理的资源

发布于 2025-01-26 16:06:56 字数 3091 浏览 3 评论 0原文

我正在创建服务器端的大型应用程序,并希望正确发布托管和未管理的资源。我是大麻的初学者,不确定我所做的是否正确。请给我一些指示。谢谢

//这是我的城市模型服务

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文