[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

AppDomain for remoted classes and windows service

javatopia

10/19/2004 5:13:00 PM

Hello,

I have a windows service that sets up the remoting server context for a
remoted class (a facade for a series of singleton classes). The
client-server part seems to work, but when I try to access the remoted data
from the windows service, it is not there. It is as if the singleton has
been re-instantiated, which makes me think the remote context runs in its own
AppDomain.

When a class is setup to be remoted, does it run in its own AppDomain,
exclusive of the primary AppDomain setting up the remoting context? If so,
do I have to use the remote client API to get at the actual data from the
windows service?

I am running framework version 1.1 SP1. My intentions are to make the
remoted data persist in a file when the windows service is stopped, and then
be reloaded when it starts again.

Maybe I should be using some other aspect of the framework to do the
persistence?? (I should be using COM+, but I like windows services)


<CODE>

class MySingletonClass : MarshalByRefObject {
private static MySingletonClass _Instance = new MySingletonClass();
public static MySingletonClass Instance { get { return(_Instance); } }
public void SaveState() { ... }
public void LoadState() { ... }
}

class MyRemoteFacade : MarshalByRefObject {
public MySingletonClass SingletonInstance {
return(MySingletonClass.Instance);
}
}

The windows service class has these two methods of interest:

public void OnStart() {
(new MyRemoteFacade()).SingletonInstance.Load(filename);
}

public void OnStop() {
(new MyRemoteFacace()).SingletonInstance.Save(filename);
}

I also tried it without using the remote facade:

public void OnStart() {
MySingletonClass.Instance.Load(filename);
}

public void OnStop() {
MySingletonClass.Instance.Save(filename);
}

Neither of which worked - the save file never contains the runtime state,
only the initial state of the singleton.

Thanks

-- Jake
jwa@javatopia.com
1 Answer

Sam Santiago

10/19/2004 6:16:00 PM

0

Sounds like you may have to override the InitializeLifetimeService method
on your remote
object and return null from it:

public class MySingleton : MarshalByRefObject
{
public override object InitializeLifetimeService()
{
return null;
}
}

By default, your remote object will have a lifetime of 5 minutes. That
means after 5 min of inactivity the current instance can be garbage
collected and new one instantiated with the next call to it. If you
override this method and expose your object as an SAO - Server Activated
Object - you'll have a true singleton. You don't have to program the
instance management into your object as in the classic singleton
implementation. If you are exposing your remote object as a CAO - Client
Activated Object - then you would want to do so.

For more extensive discussion of leases read this article:

Managing the Lifetime of Remote .NET Objects with Leasing and Sponsorship
http://msdn.microsoft.com/msdnmag/issues/03/12/LeaseManager/de...

Thanks,

Sam

--
_______________________________
Sam Santiago
ssantiago@n0spam-SoftiTechture.com
http://www.SoftiTe...
_______________________________
"javatopia" <javatopia@discussions.microsoft.com> wrote in message
news:EB32D98E-E323-4857-B4C5-EA948CBA966E@microsoft.com...
> Hello,
>
> I have a windows service that sets up the remoting server context for a
> remoted class (a facade for a series of singleton classes). The
> client-server part seems to work, but when I try to access the remoted
data
> from the windows service, it is not there. It is as if the singleton has
> been re-instantiated, which makes me think the remote context runs in its
own
> AppDomain.
>
> When a class is setup to be remoted, does it run in its own AppDomain,
> exclusive of the primary AppDomain setting up the remoting context? If
so,
> do I have to use the remote client API to get at the actual data from the
> windows service?
>
> I am running framework version 1.1 SP1. My intentions are to make the
> remoted data persist in a file when the windows service is stopped, and
then
> be reloaded when it starts again.
>
> Maybe I should be using some other aspect of the framework to do the
> persistence?? (I should be using COM+, but I like windows services)
>
>
> <CODE>
>
> class MySingletonClass : MarshalByRefObject {
> private static MySingletonClass _Instance = new MySingletonClass();
> public static MySingletonClass Instance { get { return(_Instance); } }
> public void SaveState() { ... }
> public void LoadState() { ... }
> }
>
> class MyRemoteFacade : MarshalByRefObject {
> public MySingletonClass SingletonInstance {
> return(MySingletonClass.Instance);
> }
> }
>
> The windows service class has these two methods of interest:
>
> public void OnStart() {
> (new MyRemoteFacade()).SingletonInstance.Load(filename);
> }
>
> public void OnStop() {
> (new MyRemoteFacace()).SingletonInstance.Save(filename);
> }
>
> I also tried it without using the remote facade:
>
> public void OnStart() {
> MySingletonClass.Instance.Load(filename);
> }
>
> public void OnStop() {
> MySingletonClass.Instance.Save(filename);
> }
>
> Neither of which worked - the save file never contains the runtime state,
> only the initial state of the singleton.
>
> Thanks
>
> -- Jake
> jwa@javatopia.com