Solved

.net Remoting Question

Posted on 2006-07-15
6
794 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

740 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