Remoting with "remote" objects in same AppDomain as the client

  • Thread starter Thread starter Martin Bischoff
  • Start date Start date
M

Martin Bischoff

In the MSDN article "Microsoft .NET Remoting: A Technical Overview" I
found the following statement (in the paragraph "Proxy Objects") about
method calls on remote objects:

"...the [method] call is examined to determine if it is a valid method
of the remote object and if an instance of the remote object resides in
the same application domain as the proxy. If this is true, a simple
method call is routed to the actual object."

I have created a simple remoting application which is server and client
at the same time (see below). In the sample, I instantiate the remote
object and then want to check whether it resides in the same AppDomain
as the client code.
To do so I call RemotingServices.IsObjectOutOfAppDomain(obj). I would
have expected this method to return false because client and "remote"
object are in the same AppDomain, but it returns true.


Another thing i found out is the following:

After running some rudimentary performance comparisions (using a
somewhat more complex sample), it seems to me that calling the remote
object's method takes roughly the same time no matter whether the remote
object exists in the same AppDomain or when it really is instantiated in
a different AppDomain/process (on the same machine). E.g.:

calling the remote object's method 20000 times takes:
- not measurable when called on a local instance
- 13 seconds when used as shown in the sample below
- 15 seconds when the object really is remote (different AppDomain, same
machine though)


So here are my questions:

1/ Is there a bug in RemotingServices.IsObjectOutOfAppDomain(obj)?
Shouldn't this method return false, when the "remote" object resides in
the same AppDomain as the client (as described above)?

2/ When the remote object resides in the same AppDomain as the client,
is there any optimization done by the framework (e.g. bypassing the
network stack)? My rudimentary tests made me think that there's no such
optimization, even though something like this is mentioned in the MSDN
articled cited above (at least that's how I understood the statement)


Thanks for any information.

Martin Bischoff



here's the example (example.cs):
----------------------------------
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples
{
public class HelloServer : MarshalByRefObject
{
public HelloServer()
{
Console.WriteLine("HelloServer activated");
}
public String HelloMethod(String name)
{
return "Hi there " + name;
}
}
public class Server
{
public static int Main(string [] args)
{
// server code
ChannelServices.RegisterChannel(new TcpChannel(8085));
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(HelloServer), "SayHelloSingleton",
WellKnownObjectMode.Singleton);

// client code
HelloServer obj = HelloServer)Activator.GetObject(
typeof(HelloServer), "tcp://localhost:8085/SayHelloSingleton");

System.Console.WriteLine(
"IsTransparentProxy={0}, IsOutOfAppDomain={1}",
RemotingServices.IsTransparentProxy(obj),
RemotingServices.IsObjectOutOfAppDomain(obj));
Console.WriteLine(obj.HelloMethod("server"));
return 0;
}
}
}
----------------------------------
 
Hi Martin,

I have tested your sample code and get the same result.

IsTransparentProxy=True, IsOutOfAppDomain=True
HelloServer activated
Hi there server

I am not sure about why you think that the IsOutOfAppDomain should be
false. I am sorry if there is any misunderstanding.

RemotingServices.IsTransparentProxy Method returns a Boolean value
indicating whether the given object is a transparent proxy or a real object.

RemotingServices.IsObjectOutOfAppDomain Method returns a Boolean value
indicating whether the object specified by the given transparent proxy is
contained in a different AppDomain than the object that called the current
method.

There can be more than one application domain in an application.
Application domains provide a unit of isolation for the common language
runtime. They are created and run inside a process. Application domains are
usually created by a runtime host, which is an application responsible for
loading the runtime into a process and executing user code within an
application domain. The runtime host creates a process and a default
application domain, and runs managed code inside it.

A common language runtime host creates application domains automatically
when they are needed. You can create your own application domains and load
into them those assemblies that you want to manage personally. You can also
create application domains from which you execute code. The following is a
simple sample for your reference.

using System;
using System.Reflection;
class AppDomain1
{
public static void Main()
{
Console.WriteLine("Creating new AppDomain.");
AppDomain domain = AppDomain.CreateDomain("MyDomain");

Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("child domain: " + domain.FriendlyName);
}
}

If I have misunderstood your concern, please let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi Jacob,
thanks for your comments.

I think that in my sample, there is only one AppDomain.

I also tested this using perfmon and displaying the (global) counter
".NET CLR Loading/Total Appdomains". This value increases by 1 after
every run of the sample application.

This means, that both, the client and the "remote" object reside in the
same Appdomain. Therefore I expected IsOutOfAppDomain() to return false.

Do I misunderstand something? Is there a way how I can check in which
AppDomain the client/remote object is running?

Best regards,
Martin Bischoff
 
Hi Martin,

I agree with you that in order to confirm this issue, we need to check in
which AppDomain the client/remote object is running. Unfortunately, there
is not a simple way to do it.

Based on my research and experience, we can use WinDbg and sos.dll to do it
if you want. Please refer to the following URL carefully.

http://msdn.microsoft.com/vstudio/using/understand/perf/default.aspx?pull=/l
ibrary/en-us/dnbda/html/dbgch03.asp

It demonstrates "Consider which threads are bound to which AppDomain, and
the different stages of execution".

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi Jacob,

I will try to do what you are suggesting. But isn't the approach using
performance monitor to check how many AppDomains there are also valid?
 
Hi Martin,

The approach using performance monitor to check how many AppDomains there
are is valid for checking the number of Appdomains. It is not valid to
check whether the object specified by the given transparent proxy is
contained in a different AppDomain than the object that called the current
method. Thank you for your understanding.

If you have any concerns regarding this issue, please feel free to let me
know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Hi Jacob,

since performance monitor shows only one additional Appdomain after
every start of my sample application, I can then be sure that the object
specified by the transparent proxy resides in the same AppDomain as the
client.

Therefore it seems that there's a bug in
RemotingServices.IsObjectOutOfAppDomain().

And please, do you have any information about my second question (from
the original post):
Is there any optimization in the remoting framework if the object
specified by the transparent proxy is in the same AppDomain as the
client? By optimization I mean something like bypassing the network stack.

Thanks,
Martin Bischoff
 
Hi Martin,

The TransparentProxy in your code will invoke the RealProxy as usual,
because of the code statement "Activator.GetObject".

Regarding the statement in the MSDN article that you referred, we need to
understand it by comparing the following situations:

======================
ChannelServices.RegisterChannel(new TcpChannel());
RemotingConfiguration.RegisterWellKnownClientType(

typeof(HelloService),

"tcp://localhost:8085/SayHelloSingleton"
);
HelloServer obj = new HelloServer();

As the RegisterWellKnownClientType is called, the remoting HelloServer
object is in another AppDomain, which is created by the remoting service
host, therefore, the realproxy will be invoked.
====================

ChannelServices.RegisterChannel(new TcpChannel(8085));
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(HelloServer), "SayHelloSingleton",
WellKnownObjectMode.Singleton);

HelloServer obj = new HelloServer();

As the RegisterWellKnownServiceType is called, the remoting HelloServer
object is in the same AppDomain as the actual object. Therefore, the proxy
is bypassed and the optimization is made in this case.

That's what the MSDN article wants to inform us, based on the above two
situations.

Thanks,

Santo Xin

Microsoft Online Partner Support
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Martin,

Regarding your second question, yes, .NET Remoting has some optimizations
based on different scenarios for better performance. When the proxy and
remote object are in one appDomain, the method call will be routed to the
actual object without a Channel for communication. In addition, when the
client and the server are in different appDomains in one process, it also
makes some optimizations by using some special channels.

Best regards,
Perry Deng
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top