CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 495

495

Chương 12: Dịch vụ Web XML và Remoting

using System.Runtime.Remoting.Channels.Tcp;

using System.Reflection;

public class Server {

private static void Main() {

// Sử dụng file cấu hình để định nghĩa các tùy chọn về mạng.

RemotingConfiguration.Configure("Server.exe.config");

// Lấy kênh Remoting đã được đăng ký.

TcpChannel channel =

(TcpChannel)ChannelServices.RegisteredChannels[0];

// Nạp RemoteObject.dll.

Assembly assembly = Assembly.LoadFrom("RemoteObject.dll");

// Xử lý tất cả các kiểu trong RemoteObject.dll.

foreach (Type type in assembly.GetTypes()) {

// Kiểm tra kiểu có phải là khả-truy-xuất-từ-xa hay không.

if (type.IsSubclassOf(typeof(MarshalByRefObject))) {

// Đăng ký kiểu (tên kiểu là địa chỉ URI).

Console.WriteLine("Registering " + type.Name);

RemotingConfiguration.RegisterWellKnownServiceType(

type, type.Name, WellKnownObjectMode.SingleCall);

// Xác định địa chỉ URL (kiểu được publish tại đây).

string[] urls = channel.GetUrlsForUri(type.Name);

Console.WriteLine(urls[0]);

}

}

Console.WriteLine("Press a key to shut down the server.");

Console.ReadLine();

}