From d3e8b6297012eb23f36fa003c4b7c8af60438ed6 Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Mon, 26 Sep 2022 21:52:18 +0800 Subject: [PATCH] fix: fix warnings in WebHdfs.Extensions.FileProviders --- .../WebHdfsDirectoryContents.cs | 22 ++++++++++++++++++- .../WebHdfsFileInfo.cs | 18 ++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsDirectoryContents.cs b/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsDirectoryContents.cs index 394ed0a3..96458dfc 100644 --- a/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsDirectoryContents.cs +++ b/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsDirectoryContents.cs @@ -16,6 +16,7 @@ namespace WebHdfs.Extensions.FileProviders public class WebHdfsDirectoryContents : IDirectoryContents, IDisposable { private readonly WebHdfsFileInfo directoryInfo; + private bool disposedValue; public WebHdfsDirectoryContents(WebHdfsFileInfo directoryInfo) { @@ -31,7 +32,11 @@ public IEnumerator GetEnumerator() { FileStatuses = new { +#if NETSTANDARD2_0 + FileStatus = Array.Empty(), +#else FileStatus = new WebHdfsFileStatus[0], +#endif }, }).FileStatuses.FileStatus; @@ -48,7 +53,22 @@ IEnumerator IEnumerable.GetEnumerator() public void Dispose() { - this.directoryInfo.Dispose(); + // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中 + this.Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!this.disposedValue) + { + if (disposing) + { + this.directoryInfo.Dispose(); + } + + this.disposedValue = true; + } } } } diff --git a/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs b/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs index e1d56c16..3734224c 100644 --- a/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs +++ b/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs @@ -18,6 +18,7 @@ public class WebHdfsFileInfo : IFileInfo, IDisposable private readonly HttpClient httpClient; private readonly UriBuilder fileWebHdfsUriBuilder; private WebHdfsFileStatus fileStatus; + private bool disposedValue; public WebHdfsFileInfo(Uri nameNodeUri, string relativePath) { @@ -81,7 +82,9 @@ public void Refresh() public void Dispose() { - this.httpClient.Dispose(); + // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中 + this.Dispose(disposing: true); + GC.SuppressFinalize(this); } internal async Task GetFileStatuses() @@ -110,6 +113,19 @@ internal async Task GetFileStatuses() } } + protected virtual void Dispose(bool disposing) + { + if (!this.disposedValue) + { + if (disposing) + { + this.httpClient.Dispose(); + } + + this.disposedValue = true; + } + } + private static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds) { #if _NET462 || _NETSTANDARD2_0