Remoting's & ldquo; passed references & rdquo; understanding, remoting passed

Source: Internet
Author: User

Remoting's "passed references" understanding, remoting transfers

WCf is a mass integrator and has many other Microsoft technologies, among which Remoting is widely used in distributed architecture. Therefore, it is helpful to understand the concept of Remoting.

When Remoting is mentioned, it has to involve the MarshalByRefObject object. The explanation of the word on the network is very vague. In particular, "cross-origin access, Remoting is a reference transfer, not a value transfer ", instead, I didn't talk about what the "reference transfer" is. In some cases, I did talk about how to pass all the content that requires interaction through the ObjRef object, however, we do not have a clear understanding of "reference transfer", or we use other methods to understand "reference transfer" rather than "reference transfer" to better understand Remoting, this logical way of thinking is especially prone to vague concepts, which seem to be clear, but actually do not understand.

An example is written to understand the reference transfer, which may help you understand the code.

Define Interface

public interface IPersonService    {        String HelloMethod(String name);    }

Define Implement

public class PersonService : MarshalByRefObject, IPersonService{    public String HelloMethod(String name)    {        Console.WriteLine(            "Server Hello.HelloMethod : {0}", name);        return "Hi there " + name;    }}

Define Service
Static void Main (string [] args) {// TcpChannel chan1 = new TcpChannel (8085 ); // for remote calls, the client channel HttpChannel chan2 = new HttpChannel (8086) that uses HTTP to transmit messages ); // provides static methods to help you remotely process channel registration, resolution, and URL discovery. This class of ChannelServices cannot be inherited. registerChannel (chan1, false); ChannelServices. registerChannel (chan2, false); // provides a variety of static methods for remote Structure Configuration RemotingConfiguration. registerWellKnownServiceType (// typeof (HelloServer), typeof (PersonService), "SayHello", WellKnownObjectMode. singleton); System. console. writeLine ("Press Enter key to exit"); System. console. readLine ();}
Define Client
Class Program {static void Main (string [] args) {TcpChannel chan1 = new TcpChannel (); ChannelServices. registerChannel (chan1, false); // Activator contains specific methods for creating object types locally or remotely, or obtaining references to existing remote objects. Unable to inherit this class IPersonService obj1 = (IPersonService) Activator. getObject (typeof (IPersonService), // typeof (HelloServer), "tcp: // localhost: 8085/SayHello"); if (obj1 = null) {System. console. writeLine ("cocould not locate TCP server");} // use the HTTP channel to obtain the remote object HttpChannel chan2 = new HttpChannel (); ChannelServices. registerChannel (chan2, false); IPersonService obj2 = (IPersonService) Activator. getObject (typeof (IPersonService), // typeof (HelloServer), "http: // localhost: 8086/SayHello"); if (obj2 = null) {System. console. writeLine ("cocould not locate HTTP server");} Console. writeLine ("Client1 TCP HelloMethod {0}", obj1.HelloMethod ("Caveman1"); Console. writeLine ("Client2 HTTP HelloMethod {0}", obj2.HelloMethod ("Caveman2"); Console. readLine ();}}

Note that the "IPersonService" interface used by the client does not know the instance class of the interface for the client,

Client method calls only package the call information and link information through the proxy object generated by Activator,

After receiving the packaged information, the server performs operations on the corresponding classes and methods based on the packaging information, such as creating (calling) service entities and calling methods, package the returned values and transmit them to the client. Further research is required on the specific logic.

Here, I just want to explain what the meaning of "Remoting transmission reference" means, so as not to misunderstand it.


What is the difference between value transfer and reference transfer? Why is it true that only values are transmitted in Java? What should I do during the interview?

The value transfer only copies the parameter. The function operates on another parameter, but the value of another parameter is the same as that of the passed parameter.
The address transfer directly transmits the parameter address to the function. In this case, the function directly operates on the original parameter. So the value will change.
For more information about how to pass only values, see www.javaeye.com/topic/214404.

C # do not understand the transfer of reference types

This problem is a bit complicated to explain. I will give some examples to illustrate it.

First, we define an object for testing.
Public class TestClass
{
Public int Number {get; set ;}
}

Then define several functions to see the parameter passing of the so-called reference type.

Class Program
{
Static void Main (string [] args)
{
TestClass test = new TestClass ();
Console. WriteLine ("default: {0}", test. Number );
Function1 (test );
Console. WriteLine ("after Function1: {0}", test. Number );
Function2 (test );
Console. WriteLine ("after Function2: {0}", test. Number );
Function3 (ref test );
Console. WriteLine ("after Function3: {0}", test. Number );
Function4 (out test );
Console. WriteLine ("after Function4: {0}", test. Number );
Console. ReadKey ();

}

Static void Function1 (TestClass obj)
{
Obj. Number = 1;
}

Static void Function2 (TestClass obj)
{
Obj = new TestClass ();
Obj. Number = 2;
}

Static void Function3 (ref TestClass obj)
{
Obj = new TestClass ();
Obj. Number = 3;
}

Static void Function4 (out TestClass obj)
{
Obj = new TestClass ();
Obj. Number = 4;
}

}

Execute this code. Let's take a look at the result:

Default: 0
After Function1: 1
After Function2: 1
After Function3: 3
After Function4: 4

It can be seen that only Function2 has not achieved our expected results. Why? Is it passed by reference?

I think this is the problem the landlord is facing.

Now let's take this... the rest of the full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.