I need to use cross-appdomain calls in my app, and sometimes I have this RemotingException:
Object '/2fa53226_da41_42ba_b185_ec7d9c454712/ygiw+xfegmkhdinj7g2kpkhc_7.rem' has been disconnected or does not exist at the server.
The target object is still alive, I have checked it.
UPD I've set breakpoint in the finalizer of the target object, and it never hits. Thus, this object is alive and wasn't GC'ed.
I had the same problem and I searched for many hours with help of many StackOverflow posts.
I finally found the complete issue.
Here is the complete explanation and use case :
My class "Loader" inherits from MarshalByRefObject, and I keep it alive with a ISponsor class. I know "ClientSponsor" exists in .NET, but I had no way to determine if and when Renewal() is called, so I made my class with help of StackOverflow community (read code comments) :
And then I used this "custom sponsor" like this:
The PluginProxy type has a reference towards the real plugin type. Indeed, the PluginProxy is instanciated inside Plugin AppDomain, and returned to main AppDomain, to allow it to call Plugins even if it ignore their real type. So the PluginProxy, to be accessible from main AppDomain, have to be serialized to cross AppDomains limits. I had a problem because I didn't sponsored these MarshalByRefObject(s) :
It was a difficult bunch of problems to solve, hope this helps !
References:
StackOverflow: Remoting sponsor stops being called
StackOverflow: How do the ISponsor and ILease interfaces work?
In my case, this was happening with SQL LocalDB stored in
App_Data
folder insideWeb
project. Whenever I try to use Package Console to runupdate-database
to init my Entity Framework database using migrations, nothing happens. Then after a while, I get that error.I solved this by revising file permissions on
App_Data
. Once fixed, voila, it worked.in my case, the problem was that in the client computer, there was an virtual network adapter active, disabling the virtual network adapters, the problem was solved
This is because the Lifetime management on the server side disconnects the object when its lease expires, to allow GC to collect it. If you try to use it from the client side, you will get an Exception, even if it has not been GC'd on the server yet (e.g. because there still is another reference to it) but the lease has expired. This is to avoid unpredictable behaviour. The accepted answer provides a good reference on how to correctly manage the lifetime of Remote .NET Objects.
This happened for us because we had a static variable in one of our classes that was of type AppDomain. The class was used in a long running windows service. AppDomain has a InitializeLifetimeService method that needs to be overridden like this:
We were constantly using this as the private variable that loaded and unloaded some dlls for custom built outside logic. The answer has been taken from here: msdn answer
Because we were not able to change this at production time, we ended with a compromise of restarting the windows service at random intervals that are shorter than the lifetime of the static AppDomain variable which by trial and error we found that it is several days.
This question also helped clarify some things about lifetime: stackoverflow-question
That is probably because the local garbage collector at the server side collects the object. You can prevent that by renewing the leasing. You can read more about that in these articles:
Update: Unfortunately, the MSDN Magazine issues from 2008 or older are no longer browseable online, but only as .chm files that you have to download to your local machine. The previous issues can be found in: