locked
remoting: pass existing reference instead of new

    Question

  • please forgive my newbie-ness ...

    i have a server, implemented by:

    '+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ 
            'Create an instance of a channel 
            serverChannel = New System.Runtime.Remoting.Channels.Ipc.IpcServerChannel("csif"
            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(serverChannel, False
     
            'Register as an available service with the name remote 
            System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType( _ 
                GetType(scriptComm), "remote", System.Runtime.Remoting.WellKnownObjectMode.Singleton) 
            '+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ 

    when a client connects, i want to:

    A.  pass an existing reference to "scriptComm" that i have created

    OR

    B.  when the service creates a reference to "scriptComm", i want to be able to modify it before the service sends it to the client (i.e. load up variables, etc.). 


    any idea how to do that?
    thanks
    Monday, February 23, 2009 4:49 PM

Answers

  • You should be able to do one of two things to initialize the values of your singleton object.

    The simplest thing you can do is just add the initialization logic to the default constructor of your remote type CommonSerialInterface.scriptComm. The default constructor is called when the server starts and allows you to change the state of the object before any remote calls are made.

    If for some reason you can't do that, you can use a workaround by calling a client on your server right after you start the server to perform the initialization logic. For example:

                ChannelServices.RegisterChannel(new TcpChannel(8088));
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(StringHolder), "myobject.rem", WellKnownObjectMode.Singleton);
                Console.WriteLine("Service started");
                var sh = Activator.GetObject(typeof(RemotingServer.StringHolder), "tcp://localhost:8088/myobject.rem") as RemotingServer.StringHolder;
                sh.s = "Hello with a TWIST";
                Console.ReadKey();
    Here you change a variable on your remote object by calling the client right after the server starts up.
    Thursday, June 11, 2009 12:22 AM
    Moderator

All replies

  • Sounds like you want Singleton instancing, and furthermore you're already using it. -- all clients connect to the same instance of the server object. Is that not getting the behavior you want?
    Brain.Save() -- http://hyperthink.net/blog
    Thursday, February 26, 2009 7:57 PM
    Moderator
  • Visit

     http://www.codeproject.com/KB/IP/Remoting_Architecture.aspx

    One if the possible way to control the object flow and add some details to it is to add costom zinks. Or use the dynamic property. or if you just want it as a singleton object.then make the the serveractivation type as singleton instead of single call

    Friday, February 27, 2009 9:04 AM
  • thanks for the replies.
    it is true, i am using singleton behavior.  this, unfortunately, is not giving me the behavior i want.  remoting does not pass the existing instance of scriptComm that i want, it passes a new object.  i know this because there are some variables i set up when i create scriptComm that are not set up when i get the instance in my client. 

    basically, i created scriptComm to be able to access some internal features of my application in a controlled way, and the variables i set up are references to instances of other classes in the application.  everytime i try to connect to scriptComm using Remoting, i always get the error "variable not set to instance of object" when calling a function that accesses those variables.  i can access other functions that do not access those pre-set up variables and they work fine, so i am connecting properly, just getting a New instance rather than the existing one.

    here is the code i use to connect:

    rClient = New System.Runtime.Remoting.Channels.Ipc.IpcClientChannel 
    System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(rClient, False
     
    System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType( _ 
          GetType(CommonSerialInterface.scriptComm), "ipc://csif/remote"
     
    csif = System.Activator.GetObject(GetType( _ 
          CommonSerialInterface.scriptComm), "ipc://csif/remote"


    thanks...


    Thursday, March 12, 2009 4:22 PM
  • You should be able to do one of two things to initialize the values of your singleton object.

    The simplest thing you can do is just add the initialization logic to the default constructor of your remote type CommonSerialInterface.scriptComm. The default constructor is called when the server starts and allows you to change the state of the object before any remote calls are made.

    If for some reason you can't do that, you can use a workaround by calling a client on your server right after you start the server to perform the initialization logic. For example:

                ChannelServices.RegisterChannel(new TcpChannel(8088));
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(StringHolder), "myobject.rem", WellKnownObjectMode.Singleton);
                Console.WriteLine("Service started");
                var sh = Activator.GetObject(typeof(RemotingServer.StringHolder), "tcp://localhost:8088/myobject.rem") as RemotingServer.StringHolder;
                sh.s = "Hello with a TWIST";
                Console.ReadKey();
    Here you change a variable on your remote object by calling the client right after the server starts up.
    Thursday, June 11, 2009 12:22 AM
    Moderator
  • This seems tobe what you are looking for:

    TcpChannel channel = new TcpChannel(1234);
                ChannelServices.RegisterChannel(channeltrue);
     
                DialogService myserv = new DialogService(Task.ActivityType);
                System.Runtime.Remoting.RemotingServices.Marshal(myserv"DialogService"typeof(IDialogService));
    • Proposed as answer by LunicLynx Monday, April 18, 2011 3:26 PM
    Monday, April 18, 2011 3:26 PM