Dispose unmanaged resources (configuration, fonts). GC.SuppressFinalize
What is unmanaged resource at all? Its a resource of operation system outside a .NET framework world: File, Memory to store fonts, network connection, textures, db connection and other. Some unmanaged resource is present always for any .NET Framework program (even smallest as possible), like fonts or configuration files.
After using any unmanaged resource you can realize it by implementation IDisposable interface and in some case you can call GC.SupressFinalize() to send message to Garbage Collector (GC) that you realize resource manually and full.
Below you can see main example to understand this sentence as programmer.
1: Imports System.Configuration
2: Imports System.Reflection
3:
4: Public MustInherit Class AppConfig
5: Implements IDisposable
6:
7: Public Shared Function Change(ByVal path As String) As AppConfig
8: Return New ChangeAppConfig(path)
9: End Function
10:
11: Public MustOverride Sub Dispose() Implements IDisposable.Dispose
12:
13: Private Class ChangeAppConfig
14: Inherits AppConfig
15:
16: Private ReadOnly OldConfig As String = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString()
17: Private DisposedValue As Boolean
18:
19: Public Sub New(ByVal path As String)
20: AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path)
21: ResetConfigMechanism()
22: End Sub
23:
24: Public Overrides Sub Dispose()
25: If Not DisposedValue Then
26: AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", OldConfig)
27: ResetConfigMechanism()
28: DisposedValue = True
29: End If
30: GC.SuppressFinalize(Me)
31: End Sub
32:
33: Private Shared Sub ResetConfigMechanism()
34: GetType(ConfigurationManager).
35: GetField("s_initState", BindingFlags.NonPublic Or BindingFlags.Static).
36: SetValue(Nothing, 0)
37:
38: GetType(ConfigurationManager).
39: GetField("s_configSystem", BindingFlags.NonPublic Or BindingFlags.Static).
40: SetValue(Nothing, Nothing)
41:
42: GetType(ConfigurationManager).Assembly.GetTypes().
43: Where(Function(x) x.FullName = "System.Configuration.ClientConfigPaths").
44: First().
45: GetField("s_current", BindingFlags.NonPublic Or BindingFlags.Static).
46: SetValue(Nothing, Nothing)
47: End Sub
48: End Class
49:
50:
51: End Class
52:
In program below in any place outside a Using you see normal unmanaged resource (app.config) - configuration file for start .NET program, but inside using you can work only in relocated app.config.
162: Dim OldConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
163: Dim CurConnectionString As String = OldConfig.ConnectionStrings.ConnectionStrings("FolexDb").ConnectionString
164: Using AppConfig.Change(IO.Path.Combine(TempDir, "App.config"))
165: Dim NewConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
166: NewConfig = OldConfig
167: NewConfig.ConnectionStrings.ConnectionStrings.Remove("FolexDb")
168: NewConfig.ConnectionStrings.ConnectionStrings.Add(New ConnectionStringSettings With {
169: .Name = "FolexDb",
170: .ProviderName = "System.Data.EntityClient",
171: .ConnectionString = CurConnectionString.Replace("C:\Users\khark\AppData\Local\Folex-1\FolexDB.sqlite", FolexDbLocation)})
172: NewConfig.SaveAs(IO.Path.Combine(TempDir, "App.config"), ConfigurationSaveMode.Modified)
173: ConfigurationManager.RefreshSection("connectionStrings")
174: db1 = New FolexDb
175: End Using
This pattern is very useful to moving app.config to temporary dir and place database in temporary dir.
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |