diff --git a/LibGit2Sharp.Tests/SmartSubtransportFixture.cs b/LibGit2Sharp.Tests/SmartSubtransportFixture.cs index 9d71d3f3a..93be33c4b 100644 --- a/LibGit2Sharp.Tests/SmartSubtransportFixture.cs +++ b/LibGit2Sharp.Tests/SmartSubtransportFixture.cs @@ -70,8 +70,7 @@ public void CustomSmartSubtransportTest(string scheme, string url) } finally { - GlobalSettings.UnregisterSmartSubtransport(registration); - + registration.Dispose(); ServicePointManager.ServerCertificateValidationCallback -= certificateValidationCallback; } } @@ -89,22 +88,10 @@ public void CannotReregisterScheme() } finally { - GlobalSettings.UnregisterSmartSubtransport(httpRegistration); + httpRegistration.Dispose(); } } - [Fact] - public void CannotUnregisterTwice() - { - SmartSubtransportRegistration httpRegistration = - GlobalSettings.RegisterSmartSubtransport("http"); - - GlobalSettings.UnregisterSmartSubtransport(httpRegistration); - - Assert.Throws(() => - GlobalSettings.UnregisterSmartSubtransport(httpRegistration)); - } - private class MockSmartSubtransport : RpcSmartSubtransport { protected override SmartSubtransportStream Action(String url, GitSmartSubtransportAction action) diff --git a/LibGit2Sharp/GlobalSettings.cs b/LibGit2Sharp/GlobalSettings.cs index 1a52089d7..f07a549f2 100644 --- a/LibGit2Sharp/GlobalSettings.cs +++ b/LibGit2Sharp/GlobalSettings.cs @@ -78,21 +78,6 @@ public static SmartSubtransportRegistration RegisterSmartSubtransport(stri return registration; } - /// - /// Unregisters a previously registered - /// as a custom smart-protocol transport with libgit2. - /// - /// The type of SmartSubtransport to register - /// The previous registration - public static void UnregisterSmartSubtransport(SmartSubtransportRegistration registration) - where T : SmartSubtransport, new() - { - Ensure.ArgumentNotNull(registration, "registration"); - - Proxy.git_transport_unregister(registration.Scheme); - registration.Free(); - } - /// /// Registers a new to receive /// information logging information from libgit2 and LibGit2Sharp. diff --git a/LibGit2Sharp/SmartSubtransportRegistration.cs b/LibGit2Sharp/SmartSubtransportRegistration.cs index 8fa403d37..d541a9e97 100644 --- a/LibGit2Sharp/SmartSubtransportRegistration.cs +++ b/LibGit2Sharp/SmartSubtransportRegistration.cs @@ -9,9 +9,11 @@ namespace LibGit2Sharp /// under a particular scheme (eg "http"). /// /// The type of SmartSubtransport to register - public sealed class SmartSubtransportRegistration + public sealed class SmartSubtransportRegistration : IDisposable where T : SmartSubtransport, new() { + private bool disposed = false; + /// /// Creates a new native registration for a smart protocol transport /// in libgit2. @@ -57,6 +59,38 @@ internal void Free() RegistrationPointer = IntPtr.Zero; } + void Dispose(bool disposing) + { + if (!disposed) + { + if (disposing) + { + // dispose managed objects + } + // dispose unmanaged objects + Proxy.git_transport_unregister(Scheme); + Free(); + disposed = true; + } + } + + /// + /// Clean up resources + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Finalizer called to free unmanaged resources + /// + ~SmartSubtransportRegistration() + { + Dispose(false); + } + private static class EntryPoints { // Because our GitSmartSubtransportRegistration structure exists on the managed heap only for a short time (to be marshaled