Einfaches Beispiel zu .Net Remoting

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier mal ein einfaches Beispiel zu Remoting unter .Net:

Ich veröffentliche einen Service der einen String als Argument rein bekommt, einfach _Server anhängt und diesen wieder an den Client zurückschickt.
Dabei verwende ich binäres .Net Remoting via TCP, den Code gibts im Anhang.
Die Konfiguration erledige ich im Beispiel Programatisch es gibt natürlich auch die Möglichkeit das ganze Remoting Subsystem via XML und entsprechende .config Dateien zu Konfigurieren.

Das Client Projekt hat eine Referenz auf das Server Projekt (weil sie das IService Interface gemeinsam nutzen, ich "echten" Anwendungen würde man das natürlich in eine eigenes Interface Projekt legen...)

Unser Service Interface
C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace De.Tutorials.Training.Remoting.Service
{
    public interface IService
    {
        string Operation(string arg);
    }
}

Unsere Service Implementierung:
C#:
using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels.Tcp;
using De.Tutorials.Training.Remoting.Service;

namespace De.Tutorials.Training.Remoting.Server.Service.Internal
{
    public class SimpleService : MarshalByRefObject, IService
    {
        static SimpleService()
        {
            Console.WriteLine("Init SimpleService");
        }

        #region IService Members

        public string Operation(string arg)
        {
            Console.WriteLine("Server: " + arg);
            return arg + "_Server";
        }

        #endregion
    }
}

Unser Server BootStraper:
C#:
using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels.Tcp;
using De.Tutorials.Training.Remoting;
using De.Tutorials.Training.Remoting.Server.Service.Internal;

namespace De.Tutorials.Training.Remoting.Server
{
    public class ServerLauncher
    {
        public static void Main(string[] args)
        {
            
            TcpChannel channel = new TcpChannel(9898);
            ChannelServices.RegisterChannel(channel,false);
            RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
            RemotingConfiguration.ApplicationName = "Server";
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(SimpleService), "service", WellKnownObjectMode.SingleCall);

            Console.WriteLine("Server Ready!");
            Console.WriteLine("Press enter for shutdown.");
            Console.ReadLine();

            ChannelServices.UnregisterChannel(channel);
        }
    }

   
}

Unser Client:
C#:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using De.Tutorials.Training.Remoting.Service;

namespace De.Tutorials.Training.Remoting
{
    public class Client
    {
        public static void Main(string[] args)
        {

            Console.WriteLine("Press enter to start...");
            Console.ReadLine();
            TcpChannel channel = new TcpChannel(0);
            ChannelServices.RegisterChannel(channel,false);
            IService service = (IService)Activator.GetObject(typeof(IService), "tcp://localhost:9898/Server/service");
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Response from Server: " + service.Operation("Hallo->" +i)); 
                Thread.Sleep(100);
            }
        }
    }
}

Ausgabe Server:
Code:
Server Ready!
Press enter for shutdown.
Init SimpleService
Server: Hallo->0
Server: Hallo->1
Server: Hallo->2
Server: Hallo->3
Server: Hallo->4
Server: Hallo->5
Server: Hallo->6
Server: Hallo->7
Server: Hallo->8
Server: Hallo->9
Server: Hallo->10
...

Ausgabe Client:
Code:
C:\Dokumente und Einstellungen\Tom\Eigene Dateien\Visual Studio 2005\Projects\De.Tutorials.Training\De.Tutorials.Training.Remoting.Client\bin\Debug>De.Tutorials.Training.Remoting.Client.exe
Press enter to start...

Response from Server: Hallo->0_Server
Response from Server: Hallo->1_Server
Response from Server: Hallo->2_Server
Response from Server: Hallo->3_Server
Response from Server: Hallo->4_Server
Response from Server: Hallo->5_Server
Response from Server: Hallo->6_Server
Response from Server: Hallo->7_Server
Response from Server: Hallo->8_Server
Response from Server: Hallo->9_Server
...

Die Tage zeig ich dann mal wie man eigene Sinks in die Verarbeitungskette des .Net Remoting Subsystem einfügen kann (Beispielsweise für Authentifizierung, Verschlüsselung, usw...).

Gruß Tom
 

Anhänge

  • De.Tutorials.Training.Remoting.zip
    64,6 KB · Aufrufe: 1.210

Neue Beiträge

Zurück