Solved

.net Remoting Question

Posted on 2006-07-15
6
790 Views
Last Modified: 2010-08-05
I just read the remoting documentation for .net 2.0 for IpcServerChannel/IpcClientChannel.  I see that the client accesses an object that exists in the memory of the server.  Is there any way for the server to access this object as well, or does the host just hold objects that are modified by the different clients?
0
Comment
Question by:kayhustle
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17116547
Yes, just because an object is remotable doesn't mean you have to access it remotely. The server has full access to the object it is hosting. A typical configuration would look something like this:

Server:
Remoteable Object
Host Service / Application (in charge of hosting the remoted object)

Client:
Client application to access remote object
Object Interface (the interface the client will use to access the remote object)

You can put the remoted object in the GAC on the server. Then, any app on the server can access it including your host service.

Thanks.

Jason
0
 
LVL 1

Author Comment

by:kayhustle
ID: 17117469
Ok, can you show me an example of how I would access the remoted object from the server?
0
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17117492
Since the object is sitting on the server, applications on the server can make direct references to it. Simply add a reference to the object in your project and instanciate it like any other class:

You have the three following items:
1) Host Service and remoteable object (on server)
2) Client application accessing the object remotely (on workstation)
3) Server application accessing the object locally (on server)

Steps to reference the object locally from the server application:
1) Add reference to project (right-click references in solution explorer and choose "Add Reference...")
2) Instanciate the object (ex: MyRemotedObject mro = new MyRemotedObject()) and make use of it.
3) Assuming your object is in the GAC on the server (since you already have it set up for remoting), the only thing left is to deploy your app and it will find the object in the GAC... no need to deploy the object in your app's folder.

One caveat is that if you want your app on the server to use the same instance as the clients accessing it remotely (i.e. a singleton object) then you should access it remotely from the server as if your app was sitting on a workstation. Just set up your server app the same way you did your client app.

Jason
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:kayhustle
ID: 17117537
If I call RemotingConfiguration.RegisterWellKnownClientType in the server after having called RemotingConfiguration.RegisterWellKnownServiceType on the same type I get an exception saying that it has already been registered.  If I instantiate an instance of the class inside my server code, it will not be the same object as the one accessible to the clients.  Here is a code sample of my server:

public static void Main()
    {
        IDictionary properties = new Hashtable();
        properties.Add("portName", "remote");
        properties.Add("secure", true);
        properties.Add("impersonate", true);
        // Create and register an IPC channel
        IpcServerChannel serverChannel = new IpcServerChannel(properties,null);
        ChannelServices.RegisterChannel(serverChannel,false);

        // Expose an object
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(Counter), "counter", WellKnownObjectMode.Singleton);
        //The client is not accessing the same Counter that was just declared here
        Counter c = new Counter();
        c.text = "YO YO YO YO!";
       
        Console.WriteLine("Listening on {0}", serverChannel.GetChannelUri());
        ThreadPool.QueueUserWorkItem(new WaitCallback(go));
        Console.ReadLine();
    }
    private static void go(object o)
    {
        IDictionary properties = new Hashtable();
        properties.Add("secure", true);
        properties.Add("connectionTimeout", 5000);
        properties.Add("tokenImpersonationLevel", "Impersonation");


        IpcClientChannel clientChannel = new IpcClientChannel(properties, null);
        ChannelServices.RegisterChannel(clientChannel,false);
       
        //this line throws an exception if run
        RemotingConfiguration.RegisterWellKnownClientType(typeof(Counter), "ipc://remote/counter");
       
        //This counter is not the same as the previous counter that was declared in the main function.  It's text is blank
        Counter counter = new Counter();
        Console.WriteLine("text is " + counter.text);
        Console.WriteLine("This is call number {0}.", counter.Count);
        counter.text = "Time is: " + DateTime.Now.ToLongTimeString();
        ThreadPool.QueueUserWorkItem(new WaitCallback(checktext));
        Console.ReadLine();
    }
    private static void checktext(object o)
    {
        Counter counter = new Counter();
        while (true)
        {
            Console.WriteLine("Text is now: " + counter.text);
            System.Threading.Thread.Sleep(2000);
        }
    }
0
 
LVL 7

Accepted Solution

by:
whatsit2002 earned 500 total points
ID: 17117994
Ok, now I know what you're talking about. Sorry, I should have cleared a couple things up earlier. I didn't realize you were trying to use the same instance that the clients were using with a Singleton object.

To create an instance of the remote object locally on the server, try this:

Counter RObj = (Counter)Activator.GetObject(typeof(Counter), "ipc://remote/counter");

Hope that helps.

Thanks

Jason
0
 
LVL 1

Author Comment

by:kayhustle
ID: 17118125
Ok, that works, thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now