Solved

.net Remoting Question

Posted on 2006-07-15
6
798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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