C# Finalizer for .Net Framework class System.Security.Principal.WindowsIdentity
According to everything I've read, If you allocate unmanaged resources within an object one should support theSystem.IDisposable interface and free resources there. This post is to point out that in the Framework 1.1 the class System.Security.Principal.WindowsIdentity allocates a global windows handle when instantiated with a security token. This handle is not released until the destructor is called. This is contrary to your own best practice, is it not? Background: I'm implementing a secured remoting channel built on top of RPC. Since I need to create a WindowsIdentity and place this in the propertySystem.Threading.Thread.CurrentPrincipal on each remote call I consume a windows handle on each call. This works until the 32k worth of global handles is consumed waiting on the finalizers of your WindowsIdentity class. Trying to force colleciton, I tried calling GC.Collect() andGC.GetTotalMemory(true). This seemed to solve the problem at the expense of about an order of magnatude in performance. Giving up on GC to do the work for me, I'm forced to the solution below. This snipit mannually calls the destructor of the object after unregistering the instance from the pending finalizers. Error handling has been removed for simplification so this is abbreviated but roughly accurate:
MemberInfo[] members = obj.GetType().GetMember( "Finalize", MemberTypes.All, BindingFlags.NonPublic | BindingFlags.Instance );
((MethodInfo)members[0]).Invoke( obj, BindingFlags.NonPublic | BindingFlags.Instance, null, null, Thread.CurrentThread.CurrentCulture );
Frankly I'm really supprized I'm allowed to execute the code above, but it does work. This is really ugly, but what are my alternatives?
Outside of writing my own WindowsIdentity (which I do not consider an alternative) what are my options?

