Remoting C # differences between delealbyrefobject and serializable

Source: Internet
Author: User

Classes of the two methodsAverageIt is used for remote transmission (I don't want to discuss it because there is no difference)

Marshalbyrefobject transmits serializable through reference through value transfer. Now we will analyze what is reference transfer and what is value transfer.

It is important to understand remoting or WebService.

Marshalbyrefobject (reference) is the same instance on the local machine or server, but it is the object that you used locally after the server is created. For example, if Class A inherits marshalbyrefobject, then Class A is created by the server, and the client can use this instance.

Now let's assume that Class A has a method named A, and the function returns a value of the string type. This method has a series of operations. When the client calls this method, only one value is returned from the server. All the operations will be completed on the server. This is called the hacker client.

Serializable (value type) is different. Assume that the funciton method of Class A needs a Class B as the parameter, and B is a serializable class
[Serializable ()] is added to the class definition. If this method is not added, an error is returned. Let's explain through a remoting example.

First, write a class that inherits marshalbyrefobject.

Public class helloserver: marshalbyrefobject
{
Public helloserver ()
{Console. writeline ("helloserver activated ");}

Public String hellousermethod (User user)
{
String title;
If (user. Male)
Title = "Mr ";
Else
Title = "ladies ";

Console. writeline ("server hello. hellomethod: Hello, {0} {1}", user. name, title );

Return "hello," + User. Name + title;
}

}

Write another serializable class

[Serializable]
Public class user
{
Public user (string name, bool male)
{
This. Name = Name;
This. Male = male;
}
String name = "";
Bool male = true;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Public bool male
{
Get {return male ;}
Set {male = value ;}
}

}

Now we will use them on the server and client.

The server is as follows:

Public class Server
{
Public static int main (string [] ARGs)
{

Tcpchannel chan1 = new tcpchannel (8085 );
Httpchannel chan2 = new httpchannel (8086 );

Channelservices. registerchannel (chan1 );
Channelservices. registerchannel (chan2 );

Remotingconfiguration. registerwellknownservicetype (typeof (helloserver), "sayhello", wellknownobjectmode. Singleton); // create a class instance

System. Console. writeline ("Press ENTER key to exit ");
System. Console. Readline ();
Return 0;
}

The client is as follows:

Public class client
{
Public static void main (string [] ARGs)
{
// Obtain the remote object through the HTTP Channel
Httpchannel chan2 = new httpchannel ();
Channelservices. registerchannel (chan2 );
Helloserver obj1 = (helloserver) activator. GetObject (
Typeof (remotingsamples. helloserver ),
" Http: // localhost: 8086/sayhello ");// Create a class instance
If (obj1 = NULL)
{
System. Console. writeline (
"Cocould not locate HTTP server ");
}
Console. writeline (
"Client1 TCP hellousermethod {0 }",
Obj1.hellousermethod (new user ("Zhang Sheng", true); // uses the class as a parameter.
(The user must be serializable as the parameter )}

}

}

Classes derived from externalbyrefobject and classes with [serializable] can span applications.ProgramFields are passed as parameters.
Classes derived from externalbyrefobject are sent by reference. classes with the [serializable] flag are sent by value.
If this class is derived from marshalbyrefobject, the [serializable] flag is also sent by reference.

There are three cases of serialization:

    1. Serialized in XML format:
      In WebService, write a web method and pass a custom class as a parameter. This is the case. The system will help you to convert the custom class to the default XML format.
    2. Serialized to binary:
      To add the [serializable] flag, you can serialize both private and public variables.
    3. Serialized to the soap format:
      You need to implement the iserializable interface, define the serialization function iserializable. getobjectdata, and restore the serialized constructor.
      Sample of a soap parameter class:
[Serializable]
Public Class Serialze: iserializable
{
// Serialization function called by soapformatter during serialization
Void iserializable. getobjectdata (serializationinfo, streamingcontext
ctxt)
{
//Add each field to the serializationinfo object
Info. addvalue ("Username", Username );
Info. addvalue ("Userid", Userid );
}

// Restore serialization constructor called by soapformatter during restoration serialization
Public Serialze (serializationinfo info, streamingcontext ctxt)
{
// Restore serialized fields from the serializationinfo object
Username = ( String ) Info. getvalue ( " Username " , Typeof ( String ));
Userid = ( Int ) Info. getvalue ( " Userid " , Typeof ( Int ));
}

Public Serialze ()
{}

Public String Username;
Public Int Userid;
}
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.