Search code examples
c#serviceremotingtray

Is it possible to hold object instances via C# Remoting?


I'm new to remoting, I want to make a system tray app which will be used to perform some background tasks. I have other 2 programs which need to access the objects modified by the system tray app, and them do stuff with them.

It's more or less like this:

Tray App -> loads everything in background

Program 1 -> Displays some info about "MyObject" instantiated within Tray App

Program 2 -> Utilities for "MyObject" instantiated within Tray App, which connects to a random server elsewhere and do stuff,

But i'm lost on all the remoting concept.

I'm using this:

RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType, Constants.ServiceName, WellKnownObjectMode.Singleton);

But it seems like it's a reference to the object class itself, not an instance of it.

How can I process an Object within an application and let other apps use it? I don't want to use databases, or serialize anything in another file.


Solution

  • To register one of your existing instances and publish it on Remoting do as follows.

    First call:

    RemotingServices.SetObjecturilForMarshal(MarshalByRefObject, uri)
    

    Then call one of the following overloads:

    System.Runtime.Remoting.RemotingServices.Marshal(MarshalByRefObject)
    System.Runtime.Remoting.RemotingServices.Marshal(MarshalByRefObject, String)
    System.Runtime.Remoting.RemotingServices.Marshal(MarshalByRefObject, String, Type)
    

    That should work.