Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| public class NomClasse : MarshalByRefObject { // constructeur public NomClasse() { } // Méthodes public < Type > NomMethode1 (< paramétres >) { Return <resultat>; } public < Type > NomMethode2 (< paramétres >) { Return <resultat>; } // etc. } |
| // augmenter cote client la durée de vie de l'objet MBR (uniquement dans le cas WKO + SINGLETON) System.Runtime.Remoting.Lifetime.ILease oILease = (System.Runtime.Remoting.Lifetime.ILease)NET2CsRemoting.Server.Surface; oILease.Renew(TimeSpan.FromMinutes(10)); |
| static void Main(string[] args) { System.Runtime.Remoting.Channels.Tcp.TcpServerChannel oTcpChannel; // 1 - création d'un canal avec le n° de port oTcpChannel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(<N° port>); // 2 - enregistre le canal (RegisterChannel > méthode statique) System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(oTcpChannel, false); // 3 - association (type,objetUri,mode) System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(<Type>), "RemoteSurface", System.Runtime.Remoting.WellKnownObjectMode.Singleton); } |
| oType = (<Type>)Activator.GetObject(typeof(<Type>), "URI"); Result = oType.MethodeType(); |

| using System; using System.Collections.Generic; using System.Text; namespace NET2CsRemoting.Server { ///<summary> /// 1 - Objet MBR (objet distant) ///</summary> public class Surface : System.MarshalByRefObject { public Surface() { } public Single CalculateSquareSurface(Single Rayon) { Console.WriteLine("Execution CalculateSquareSurface"); Single Result; Result = (Single)(Rayon * Rayon); return Result; } } } |
| using System; using System.Collections.Generic; using System.Text; namespace NET2CsRemoting.Server { class Program { ///<summary> /// 2 - l'hote - permet de rendre accessible l'objet distant MBR par un canal de communication + associer une URI a cet objet ///</summary> ///<param name="args"></param> static void Main(string[] args) { System.Runtime.Remoting.Channels.Tcp.TcpServerChannel oTcpChannel; // 1 - création d'un canal avec le n° de port oTcpChannel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(60001); // 2 - enregistre le canal (RegisterChannel > méthode statique) System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(oTcpChannel, false); // --- A - WKO ---- // 3 - association (type,objetUri,mode) //System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(Surface), "RemoteSurface", System.Runtime.Remoting.WellKnownObjectMode.Singleton); // ou // System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(Surface), "RemoteSurface", System.Runtime.Remoting.WellKnownObjectMode.SingleCall); // --- B - CAO ------ activé cote client System.Runtime.Remoting.RemotingConfiguration.RegisterActivatedServiceType(typeof(Surface)); // Fin du service Console.WriteLine("Appuyer sur une touche pour stopper le serveur"); Console.ReadLine(); } } } |
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace NET2CsRemoting.UI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.CallCalcultateSquareSurface(); } // Methods public void CallCalcultateSquareSurface() { NET2CsRemoting.Server.Surface oSurface; Single Result; try { // -- A - WKO // 1 ouverture du canal de communication sur l'uri (remarque l'uri contient bien l'objetUri definie ds l'hote coté service) //oSurface = (NET2CsRemoting.Server.Surface)Activator.GetObject(typeof(NET2CsRemoting.Server.Surface), "tcp://localhost:60001/RemoteSurface"); // -- OU -- // --- B - CAO ------ activé cote client System.Runtime.Remoting.RemotingConfiguration.RegisterActivatedClientType(typeof(NET2CsRemoting.Server.Surface), "tcp://localhost:60001"); oSurface = new NET2CsRemoting.Server.Surface(); // 2 Dialogue - apppel de méthodes Result = oSurface.CalculateSquareSurface(Convert.ToSingle(textBox1.Text)); // Affichage du résultat formaté lblResult.Text = Result.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } } |