33// See the LICENSE file in the project root for more information.
44
55using System . CommandLine ;
6+ using System . Diagnostics . CodeAnalysis ;
67using System . Globalization ;
78using System . Text . Json ;
89using DevProxy . Abstractions . Proxy ;
@@ -15,7 +16,7 @@ namespace DevProxy.Abstractions.Plugins;
1516
1617public abstract class BasePlugin (
1718 ILogger logger ,
18- ISet < UrlToWatch > urlsToWatch ) : IPlugin
19+ ISet < UrlToWatch > urlsToWatch ) : IPlugin , IDisposable
1920{
2021 public bool Enabled { get ; protected set ; } = true ;
2122 protected ILogger Logger { get ; } = logger ;
@@ -64,6 +65,17 @@ public virtual Task MockRequestAsync(EventArgs e, CancellationToken cancellation
6465 {
6566 return Task . CompletedTask ;
6667 }
68+
69+ protected virtual void Dispose ( bool disposing )
70+ {
71+ // Override in derived classes to dispose managed resources
72+ }
73+
74+ public void Dispose ( )
75+ {
76+ Dispose ( true ) ;
77+ GC . SuppressFinalize ( this ) ;
78+ }
6779}
6880
6981public abstract class BasePlugin < TConfiguration > (
@@ -74,27 +86,30 @@ public abstract class BasePlugin<TConfiguration>(
7486 IConfigurationSection pluginConfigurationSection ) :
7587 BasePlugin ( logger , urlsToWatch ) , IPlugin < TConfiguration > where TConfiguration : new ( )
7688{
77- private TConfiguration ? _configuration ;
89+ #pragma warning disable CA2213 // HttpClient is injected from DI and should not be disposed by this class
7890 private readonly HttpClient _httpClient = httpClient ;
91+ #pragma warning restore CA2213
7992
8093 protected IProxyConfiguration ProxyConfiguration { get ; } = proxyConfiguration ;
94+
95+ [ AllowNull ]
8196 public TConfiguration Configuration
8297 {
8398 get
8499 {
85- if ( _configuration is null )
100+ if ( field is null )
86101 {
87102 if ( ! ConfigurationSection . Exists ( ) )
88103 {
89- _configuration = new ( ) ;
104+ field = new ( ) ;
90105 }
91106 else
92107 {
93- _configuration = ConfigurationSection . Get < TConfiguration > ( ) ;
108+ field = ConfigurationSection . Get < TConfiguration > ( ) ;
94109 }
95110 }
96111
97- return _configuration ! ;
112+ return field ! ;
98113 }
99114 }
100115 public IConfigurationSection ConfigurationSection { get ; } = pluginConfigurationSection ;
0 commit comments