I've read a fair bit on remoting but nearly all of it seems to be from the context of the child calling the server so I'm pretty confused on what I should do here.
I have a .NET 3.5 server application that compiles a set of C# scripts into a separate application domain. All calls into this application domain are synchronous and they get a newly created 'provider' object inheriting MarshalByRefObject which they can use to access functionality from the server application domain.
So I have the following heavily simplified example, running in the server appdomain:
object RunScript()
{
var myProvider = new MyProvider(this); // Inherits MarshalByRefObject
// Passes myProvider to the script appdomain
var results = CallAMethodInScriptAppDomain(myProvider);
// I want myProvider to be garbage collectable from here.
return results;
}
I did try getting it to work with sponsors and leases in the past but whatever I did it was still getting collected. I think that was because they were designed to be used from the client and I'm trying to manage the lifetime of this object from the server.
The fact is I know exactly at which line of code in the server appdomain I want it to be collected (or rather be collectable), and it must not be collected before that no matter how long the script call takes (most script calls will be in the order of a few ms, but theoretically some could take hours).
Currently my InitializeLifetimeService returns null because that was the only way I could get it to not collect while my scripts were running.
Any idea?