About remoteing Activation

Source: Internet
Author: User

When a remoteing instance is created, when will it start to work for us? When will it disappear from the memory? Can we clearly know this, can we control him as we like? These questions are what we are going to talk about today.
Activation is divided into server activation (well-known wellknonw) and client activation. Server activation includes singlecall and Singleton ).
Let's take a look at an example of Single-element activation.
Based on the previous example, we only need to make a few modifications.
First, add a counter to the DLL object. Through this calculator, we can understand the instance changes.
Using system;

Namespace remoteobject
{
Public class myobject: marshalbyrefobject
{
Private int I = 0;

Public int add (int A, int B)
{
Return A + B;
}
Public String STR ()
{
Return "I am come from server :";
}
Public int count ()
{
Return ++ I;
}

}
}
Then the client adds a process to call this counter.
Using system;

Namespace remoteclient
{
Class myclient
{
[Stathread]
Static void main (string [] ARGs)
{
Remoteobject. myobject remoteclient = (remoteobject. myobject)

Activator. GetObject (typeof

(Remoteobject. myobject), system. configuration. configurationsettings. deleettings

["Serviceurl"]);
Console. writeline (remoteclient. STR () + remoteclient. Add (1, 2 ));
Console. writeline (remoteclient. Count ());
Console. Readline ();
}
}
}
Other servers have not changed.
We run the server-side program once. Do not turn it off. Then we open a client, where 1 is displayed, another client is opened, 2 is displayed, and 3 is displayed, there are more and more numbers. As long as the server is not shut down, this number is increasing.
We are doing a monotonous example.
This is based on the above Code. Make a few modifications to the server configuration file.
<Wellknown type = "remoteobject. myobject, remoteobject" objecturi = "remoteobject. myobject"
Mode = "singlecall"/>
Then perform the same test: we run the server-side program once. Do not turn it off. Then we open a client with 1 shown above and another client with 1 shown above, if you enable it again, the value is 1. If the server is not turned off, the number is always 1. Many of my friends will be confused here, because from the experiment it seems that the number of single element objects is constantly increasing, and the monotonous objects are always the same. Actually not.
Let's take a look at the description in msdn: "The singleton object is such an object: no matter how many clients the object has, there is always only one instance and the object has a default lifetime. (The client can use the lifetime lease system to participate in the lifetime of the singleton instance. For more information, see lifetime lease .) When you configure an object as a singlecall object, the system creates a new object for each client method call. Because the client will obtain a reference to the new instance during each call, the singlecall type will not participate in the lifetime lease system ."
In fact, I have seen a book before, and I have made a clear explanation of these two types, saying: "singlecall is a monotonous object, and each call is instantiated (there will be multiple instances ), and will be destroyed by useless unit collectors. Singleton is a single-element object. It is only instantiated during the first call and remains there until the last customer releases it ".
After clarifying the server activation, let's take a look at the client activation.
In order to know the differences between client activation and server activation, we will work with the two to do a test.
Modify the remote object as follows:
Using system;

Namespace remoteobject
{
Public class myobject: marshalbyrefobject
{
Private int I = 0;

Public myobject ()
{
Console. writeline ("Activate ");
}

Public int add (int A, int B)
{
Return A + B;
}

Public int count ()
{
Return ++ I;
}
}
}
Server configuration file:
<Configuration>
<System. runtime. remoting>
<Application name = "remoteserver">
<Service>
<Activated type = "remoteobject. myobject, remoteobject"/>
</Service>
<Channels>
<Channel ref = "TCP" Port = "9999"/>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>
Client Program:
Using system;

Namespace remoteclient
{
Class myclient
{
[Stathread]
Static void main (string [] ARGs)
{
// Remoteobject. myobject APP = (remoteobject. myobject) activator. GetObject (typeof

(Remoteobject. myobject), system. configuration. configurationsettings. deleettings

["Serviceurl"]);
Remoteobject. myobject APP = (remoteobject. myobject) activator. createinstance

(Typeof (remoteobject. myobject), null, new object [] {New

System. runtime. remoting. Activation. urlattribute

(System. configuration. configurationsettings. receivettings ["serviceurl"])});
// Console. writeline (App. Count ());
Console. Readline ();
}
}
}
Client configuration file:
<Configuration>
<Deleetask>
<Add key = "serviceurl" value = "TCP: // localhost: 9999/remoteserver"/>
</Appsettings>
</Configuration>
When running the program, we can see that the server outputs "activation" when the client starts. We then turn back to the well-known mode for testing and find that only the running method outputs "activation" on the server ".

Summary:
Client activation is similar to a monotonous object. Every time a new instance is activated, the difference between client activation and server activation is that when the client method is called for the first time, is activated, that is, the DLL is registered and applied for space in the server and container (server program). If the client is activated, it is instantiated (Registration and Application Space) when the client sends a request ).
(Thanks to lovecherry's blog. I have added some ideas in my article. If you want to see the pure version of remoting, see http://www.cnblogs.com/lovecherry. Do not dare to capture the beauty of others .)

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.