#副標=分散式系統建構 #大標=: .NET Remoting實作(下) ----------程---------- (程式6) [Serializable] public class Person {  public string Name;  public int Age; } ------------------------ ----------程---------- (程式7) public Person GetPerson() {  Person data=new Person();  data.Name="code6421";  data.Age=18;  return data; } ------------------------ ----------程---------- (程式8) …….. using System.Runtime.Remoting.Channels.Tcp; …….. namespace Server { class Startup { [STAThread] static void Main(string[] args) { HttpChannel chnl=new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); TcpChannel chnl1=new TcpChannel(9012); ChannelServices.RegisterChannel(chnl1); RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyService),"MyService.soap",WellKnownObjectMode.SingleCall); System.Console.WriteLine("Remoting Server Running..."); Console.ReadLine(); } } } ------------------------ ------------------------ (程式9,Use TCP) using System.Runtime.Remoting.Channels.Tcp; ……….. static IMyService SimpleBinding() { TcpChannel chnl=new TcpChannel(); ChannelServices.RegisterChannel(chnl); return (IMyService)Activator.GetObject(typeof(IMyService),"tcp://localhost:9012/MyService.soap"); } ------------------------ ----------程---------- (程式10,Use HTTP) Using System.Runtime.Remoting.Channels.Http; ……….. static IMyService SimpleBinding() { HttpChannel chnl=new HttpChannel(); ChannelServices.RegisterChannel(chnl); return (IMyService)Activator.GetObject(typeof(IMyService),"http://localhost:1234/MyService.soap"); } ------------------------ ----------程---------- (程式11) ListDictionary properties=new ListDictionary(); properties.Add(“port”,1234); HttpChannel chnl=new HttpChannel(properties, new SoapClientFormatterSinkProvider(), new SoapServerFormatterSinkProvider()); ChannelServices.RegisterChannel(chnl); ------------------------ ----------程---------- (程式12) …………. using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels.Tcp; ………….. namespace Server { class Startup { [STAThread] static void Main(string[] args) { ListDictionary HttpChannelProperties = new ListDictionary(); HttpChannelProperties.Add("port", 1234);        SoapClientFormatterSinkProvider soapClientSink=new SoapClientFormatterSinkProvider(); BinaryClientFormatterSinkProvider binaryClientSink=new BinaryClientFormatterSinkProvider(); soapClientSink.Next=binaryClientSink; SoapServerFormatterSinkProvider soapServerSink=new SoapServerFormatterSinkProvider(); BinaryServerFormatterSinkProvider binaryServerSink=new BinaryServerFormatterSinkProvider(); SdlChannelSinkProvider sdlProvider=new SdlChannelSinkProvider(); soapServerSink.Next=binaryServerSink; sdlProvider.Next=soapServerSink; HttpChannel chnl=new HttpChannel(HttpChannelProperties,soapClientSink,sdlProvider);         ChannelServices.RegisterChannel(chnl); TcpChannel chnl1=new TcpChannel(9012); ChannelServices.RegisterChannel(chnl1); RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyService),"MyService.soap",WellKnownObjectMode.Singleton); System.Console.WriteLine("Remoting Server Running..."); Console.ReadLine(); } } } ------------------------ ----------程---------- (程式13,Use TCP+SOAP) static IMyService TcpSoapBinding() { ListDictionary prop=new ListDictionary(); prop.Add("port",9013); TcpChannel chnl=new TcpChannel(prop,new SoapClientFormatterSinkProvider(),new SoapServerFormatterSinkProvider()); ChannelServices.RegisterChannel(chnl); return (IMyService)Activator.GetObject(typeof(IMyService),"tcp://localhost:9012/MyService.soap"); } ------------------------ ----------程---------- (程式14,Use TCP+Binary) static IMyService TcpBinaryBinding() { ListDictionary prop=new ListDictionary(); prop.Add("port",9013); TcpChannel chnl=new TcpChannel(prop,new BinaryClientFormatterSinkProvider(),new BinaryServerFormatterSinkProvider()); ChannelServices.RegisterChannel(chnl); return (IMyService)Activator.GetObject(typeof(IMyService),"tcp://localhost:9012/MyService.soap"); } ------------------------ ----------程---------- (程式15) using System; using System.Runtime.Remoting; namespace Server { class Startup { [STAThread] static void Main(string[] args) { RemotingConfiguration.Configure("Server.exe.config"); System.Console.WriteLine("Remoting Server Running..."); Console.ReadLine(); } } } ------------------------ ----------程---------- (組態檔1) ------------------------ ----------程---------- (組態檔2) ………. ………… ------------------------ ----------程---------- (組態檔3,Use HTTP) ------------------------ ----------程---------- (組態檔4,Use TCP) …………………. …………………. ------------------------ ----------程---------- (組態檔5,Multi-Transport&Multi-Message Formatter Remoting Server)     ------------------------ ----------程---------- (組態檔6,Multi-Transport&Multi-Message Formatter Remoting Client)    or ------------------------ ----------程---------- (程式16) Unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses MyService; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var RemotingObj:MyServicePortType; data:Person; begin RemotingObj:=GetMyServicePortType(); Edit1.Text:=RemotingObj.HelloService('code6421'); ShowMessage('Next'); data:=RemotingObj.GetPerson; Edit1.Text:=data.Name; Edit2.Text:=IntToStr(data.Age); end; end. ------------------------ ----------程---------- (程式17) Using System; namespace Share { public interface IMyFactory { IMyService getService(); } public interface IMyService { int State { get; set; } } } ------------------------ ---------程---------- (程式18) Using System; using Share; namespace Server { public class ServiceFactory:MarshalByRefObject,IMyFactory { public IMyService getService() { return new ServiceImpl(); } } public class ServiceImpl:MarshalByRefObject,IMyService { private int _state; public int State { get { return _state; } set { _state=value; } } } } ------------------------ ----------程---------- (程式19) using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; namespace Server { class Startup { [STAThread] static void Main(string[] args) { HttpChannel chnl=new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceFactory),"factory.soap",WellKnownObjectMode.Singleton); Console.ReadLine(); } } } ------------------------ (程式20) using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using Share; namespace Client { class Startup { [STAThread] static void Main(string[] args) { HttpChannel chnl=new HttpChannel(); ChannelServices.RegisterChannel(chnl); IMyFactory factory=(IMyFactory)Activator.GetObject(typeof(IMyFactory),"http://localhost:1234/factory.soap"); IMyService obj1=factory.getService(); IMyService obj2=factory.getService(); obj1.State=15; obj2.State=30; Console.WriteLine("Object 1 State:{0}",obj1.State); Console.WriteLine("Object 2 State:{0}",obj2.State); Console.ReadLine(); } } } ------------------------