This section describes the important part of. Net remoting,Data Transmission,On the basis of the previous section, we made the following changes to the data contract:
Public Interface Imyservice
{
// 1. Returns a string to the client.
String Sayhello ( String Name );
// 2. Returns a datatable to the client.
Datatable gettable ();
// 3. Returns a persondata instance object to the client.
Persondata getperson ();
// 4. Upload a able from the client to the server
Void Clienttoserver (datatable DT );
// 5. Set of instances
Ilist < Persondata > Getpersons ();
}
As you can see, the slave server can return strings, data tables, instance objects, and a set of instances. 4 there is a way to import a data table from the client to the server, let's take a look at the simple implementation of the server.
Public Class Myserviceimpl: externalbyrefobject, sharedll. imyservice
{
Public String Sayhello ( String Name)
{
Return String . Format ( @" Message from server: " + Name );
}
// Returns a datatable to the client.
Public Datatable gettable ()
{
// Simulate a datatable in this place, which can be obtained from the database.
Datatable dt = New Datatable ();
DT. Columns. Add ( New Datacolumn ( " ID " , Typeof ( String )));
DT. Columns. Add ( New Datacolumn ( " Name " , Typeof ( String )));
Datarow row = DT. newrow ();
Row [ " ID " ] = Guid. newguid (). tostring (). toupper ();
Row [ " Name " ] = " Tom " ;
DT. Rows. Add (ROW );
Row = DT. newrow ();
Row [ " ID " ] = Guid. newguid (). tostring (). toupper ();
Row [ " Name " ] = " Jack " ;
DT. Rows. Add (ROW );
Return DT;
}
// Returns a persondata instance object to the client.
Public Sharedll. persondata getperson ()
{
Return New Sharedll. persondata ( " Mike " , 23 );
}
// Upload a able from the client to the server
Public Void Clienttoserver (datatable DT)
{
Console. writeline ( " Datatable from client: " + DT. Rows [ 0 ] [ 0 ]. Tostring ());
}
// Set of instances
Public Ilist < Sharedll. persondata > Getpersons ()
{
Ilist < Sharedll. persondata > List = New List < Sharedll. persondata > ( 2 );
List. Add ( New Sharedll. persondata ( " Mike " , 23 ));
List. Add ( New Sharedll. persondata ( " Tom " , 25 ));
Return List;
}
}
After implementation, we enable the server applicationProgram:
Remotingconfiguration. Configure ( " Server.exe. config " , False );
Console. writeline ( " Server is running... " );
Console. Read ();
Start the client call program:
Remotingconfiguration. Configure ( " Client.exe. config " , False );
Wellknownclienttypeentry [] types = Remotingconfiguration. getregisteredwellknownclienttypes ();
If (Types. Length > 0 )
{
Sharedll. imyservice Service = (Sharedll. imyservice) activator. GetObject (types [ 0 ]. Objecttype,
Types [ 0 ]. Objecturl );
Try
{
// Returns a string from the server.
String Returnstring = Service. sayhello ( " Mike " );
Console. writeline (returnstring );
// Datatable returned from the server
Datatable dt = Service. gettable ();
Console. writeline ( " Datatable from server: " + DT. Rows [ 0 ] [ 0 ]. Tostring ());
// Upload a able from the client to the server
Service. clienttoserver (DT );
// Returns the persondata (Instance Object) from the server)
Sharedll. persondata person = Service. getperson ();
Console. writeline ( " Persondata instance from server: " + Person. tostring ());
// Set of instances
Ilist < Sharedll. persondata > Persons = Service. getpersons ();
Console. writeline ( " List from server: " + Persons. Count );
}
Catch (Exception ex)
{
Console. writeline (ex. Message );
}
}
Else
Console. writeline ( " No service reigstered " );
Console. Read ();
Next let's take a look at the service call results.
Client call result
Result After the server call:
All instance code download: http://files.cnblogs.com/jackhuclan/RemotingConfig.rar