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
Solved

.net Remoting Question

Posted on 2006-07-15
6
793 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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