1. Trang chủ
  2. » Công Nghệ Thông Tin

Activator trong .net remoting

52 187 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Ứng dụng sẽ cho phép chọn trên giao diện các chức năng sau:1) – Gửi 1 thông báo nào đó tới toàn bộ client2) – Gửi 1 thông báo nào đó tới 1 client nào đó3) – Tắt toàn bộ chương trình Client4) – Shutdown toàn bộ máy đang sử dụng chương trình ClientỞ đây Tôi sẽ kết hợp Timer để thiết lập trong khoảng thời gian bao nhiêu milisecond thì sẽ gửi nhận thông tin….

Activator net remoting Ứng dụng cho phép chọn giao diện chức sau: 1) – Gửi thông báo tới toàn client 2) – Gửi thông báo tới client 3) – Tắt toàn chương trình Client 4) – Shutdown toàn máy sử dụng chương trình Client Ở Tôi kết hợp Timer để thiết lập khoảng thời gian milisecond gửi/ nhận thông tin… Giao diện Server sau: Giao diện Client: Giao diện Send Message (khi Server chọn chức gửi thông báo client tự động pop up giao diện Chỉ Active cách bấm vào nút hình không hiển thị): Khi Server thực thao tác : (như hình minh họa) Thì client nhận thông tin tương Ứng Ở thao tác: Shutdown máy client, Gửi thông điệp tới client Tôi không làm, Tôi dành cho bạn làm Chú ý cách làm tương tự Đóng cửa sổ client Rất dễ, bạn cần chuyển đổi ProcessType (enum chương trình cho Tôi tạo) bạn thực yêu cầu cách dễ dàng Các bạn ý chức Tắt client Shutdown máy client bạn nên hiển thị ListView cho phép chọn máy, chọn hết Ở Tôi làm sẵn giao diện tắt client bạn ứng dụng làm lại cho Shutdown Cấu trúc Solution: Coding tầng ProxyObject – bạn để ý enum ProcessType, ứng với chức bạn cần đổi ok: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ProxyObject { [Serializable] public enum ProcessType { CLOSE_A_CLIENT_APPLICATION=100, CLOSE_ALL_CLIENT_APPLICATION=101, SEND_MESSAGE_TO_A_CLIENT = 102, SEND_MESSAGE_TO_ALL_CLIENT = 103, SHUTDOWN_A_CLIENT_COMPUTER = 104, SHUTDOWN_ALL_CLIENT_COMPUTER = 105, NONE=113 }; [Serializable] public class ClientInfor { public string ClientName { get; set; } public ProcessType Type { get; set; } } [Serializable] public class MyProcess:MarshalByRefObject { private ArrayList listClient = new ArrayList(); public void addClient(ClientInfor client) { listClient.Add(client); } public void updateClientToClose(string clientName) { for (int i = 0; i < listClient.Count; i++) { ClientInfor c = listClient[i] as ClientInfor; if (c.ClientName.Equals(clientName, StringComparison.CurrentCultureIgnoreCase)) { c.Type = ProcessType.CLOSE_A_CLIENT_APPLICATION; break; } } } public void removeAllClient() { listClient.Clear(); } public void removeClient(ClientInfor client) { listClient.Remove(client); } public void removeClientByName(string name) { for (int i = 0; i < listClient.Count; i++) { ClientInfor c = listClient[i] as ClientInfor; if (c.ClientName.Equals(name, StringComparison.CurrentCultureIgnoreCase)) { listClient.RemoveAt(i); break; } } } public ArrayList ListClient { get { return listClient; } } public ProcessType Type { get; set; } } } Coding tầng Server: Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using ProxyObject; namespace ServerTier { public partial class Form1 : Form { public Form1() { InitializeComponent(); } TcpChannel tcpChannel = null; MyProcess myProcess = null; private void btnStart_Click(object sender, EventArgs e) { int port = 9999; if (Int32.TryParse(txtPort.Text,out port) == false) { MessageBox.Show(“Invalid Port!”); txtPort.SelectAll(); txtPort.Focus(); } if (tcpChannel != null || ChannelServices.GetChannel(“tcp”) != null) { ChannelServices.UnregisterChannel(tcpChannel); } tcpChannel = new TcpChannel(port); ChannelServices.RegisterChannel(tcpChannel,false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyProcess), “MYPROCESS”, WellKnownObjectMode.Singleton); Object o= Activator.GetObject(typeof(MyProcess), “tcp://localhost:” + port + “/MYPROCESS”); if (o != null) { myProcess = o as MyProcess; btnStart.Enabled = false; this.Text = “Start OK”; } }private void btnCloseAllClientApplication_Click(object sender, EventArgs e) { myProcess.Type = ProcessType.CLOSE_ALL_CLIENT_APPLICATION; }private void btnCloseAClientApplication_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.myProcess = myProcess; frm2.ShowDialog(); }private void btnSendMessageToAClient_Click(object sender, EventArgs e) {}private void btnShutdownAllClientComputer_Click(object sender, EventArgs e) { myProcess.Type = ProcessType.SHUTDOWN_ALL_CLIENT_COMPUTER; }private void ShutdownAClientComputer_Click(object sender, EventArgs e) {} private void btnSendMessageToAllClient_Click(object sender, EventArgs e) { myProcess.Type = ProcessType.SEND_MESSAGE_TO_ALL_CLIENT; } } } Form2.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ProxyObject; namespace ServerTier { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public MyProcess myProcess; private void Form2_Load(object sender, EventArgs e) { listView1.Items.Clear(); foreach (ClientInfor client in myProcess.ListClient) { ListViewItem itm = new ListViewItem(client.ClientName); itm.Tag = client; listView1.Items.Add(itm); } }private void btnClose_Click(object sender, EventArgs e) { myProcess.Type = ProcessType.CLOSE_A_CLIENT_APPLICATION; ListView.CheckedListViewItemCollection list = listView1.CheckedItems; foreach (ListViewItem item in list) { ClientInfor client = item.Tag as ClientInfor; myProcess.updateClientToClose(client.ClientName); } } } } Coding tầng Client Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.Remoting; using ProxyObject; namespace ClientTier { public partial class Form1 : Form { public Form1() { InitializeComponent(); } MyProcess myProcess = null; Timer myTimer = null; string myName=”x”; public string getComputerName() { return System.Environment.MachineName +”-“+ System.DateTime.Now.Millisecond; } private void btnConnect_Click(object sender, EventArgs e) { string url=”tcp://”+txtServer.Text+”:”+txtPort.Text+”/MYPROCESS”; RemotingConfiguration.RegisterWellKnownClientType(typeof(MyProcess),url ); myName = getComputerName(); myProcess = new MyProcess(); ClientInfor infor = new ClientInfor() { ClientName=myName,Type=ProcessType.NONE}; myProcess.addClient(infor); frmMessage frmMsg = new frmMessage(); int isACK = -1; myTimer = new Timer(); myTimer.Enabled = true; myTimer.Interval = 300; myTimer.Start(); myTimer.Tick += delegate { switch (myProcess.Type) { case ProcessType.CLOSE_A_CLIENT_APPLICATION: foreach (ClientInfor client in myProcess.ListClient) { if (client.ClientName.Equals(myName, StringComparison.CurrentCultureIgnoreCase) && client.Type==ProcessType.CLOSE_A_CLIENT_APPLICATION) { Close(); myProcess.removeClientByName(myName); } } break; case ProcessType.CLOSE_ALL_CLIENT_APPLICATION: Close(); myProcess.removeAllClient(); break; case ProcessType.SEND_MESSAGE_TO_A_CLIENT: break; case ProcessType.SEND_MESSAGE_TO_ALL_CLIENT: frmMsg.BackColor = Color.Red; if (isACK==-1) { isACK = 0; if (frmMsg.ShowDialog() == DialogResult.OK) { isACK = 1; } else { isACK = -1; } } break; case ProcessType.SHUTDOWN_A_CLIENT_COMPUTER: foreach (ClientInfor client in myProcess.ListClient) { if (client.ClientName.Equals(myName, StringComparison.CurrentCultureIgnoreCase) && client.Type == ProcessType.SHUTDOWN_A_CLIENT_COMPUTER) { System.Diagnostics.Process.Start(“shutdown”, “/s /t 0”); myProcess.removeClientByName(myName); } } break; case ProcessType.SHUTDOWN_ALL_CLIENT_COMPUTER: System.Diagnostics.Process.Start(“shutdown”, “/s /t 0”); myProcess.removeAllClient(); type = typeof(PrimeProxy); objURI = “PRIME_URI”; wellKnownMode = WellKnownObjectMode.Singleton; RemotingConfiguration.RegisterWellKnownServiceType(type, objURI, wellKnownMode); } catch (Exception ex) { } } private void myServerStop() { if (ChannelServices.GetChannel(“tcp”) != null) { ChannelServices.UnregisterChannel(tcpChannel); } } protected override void OnStart(string[] args) { myServerStart(); } protected override void OnStop() { myServerStop(); } } } Ở mặc định chạy port 8998, SingleTon Một vấn đề nảy sinh biết service Start nào, Stop lỗi sảy xem đâu??? Để làm điều này, Tôi hướng dẫn bạn sử dụng component Eventlog Bạn tìm tới EventLog danh mục Components, kéo thả control vào vùng Design màu xám Service1.cs Sau bạn tiến hành viết code cho eventlog, Tôi hướng dẫn bạn xem thông tin Log phần cuối, bạn xem code hoàn chỉnh mà Tôi cung cấp: using System;using System.Collections.Generic;using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using ProxyObject; namespace MyPrimeServices { public partial class MyPrimeServices8998 : ServiceBase { private TcpChannel tcpChannel = null; private int port = 8998; private Type type; private WellKnownObjectMode wellKnownMode; private string objURI; public MyPrimeServices8998() { InitializeComponent(); if (System.Diagnostics.EventLog.SourceExists(“PRIME8998”) == false) { System.Diagnostics.EventLog.CreateEventSource(“PRIME8998”, “LOG8998”); } eventLog1.Source = “PRIME8998”; eventLog1.Log = “LOG8998”; } private void myServerStart() { try { myServerStop(); //tạo kênh truyền liệu tcpChannel = new TcpChannel(port); ChannelServices.RegisterChannel(tcpChannel, false); //đăng ký remote object với Remoting framework type = typeof(PrimeProxy); objURI = “PRIME_URI”; wellKnownMode = WellKnownObjectMode.Singleton; RemotingConfiguration.RegisterWellKnownServiceType(type, objURI, wellKnownMode); eventLog1.WriteEntry(“khởi động server port ” + port.ToString() + ” lúc ” + DateTime.Now.ToString()); } catch (Exception ex) { eventLog1.WriteEntry(“Lỗi : ” + ex.Message); } } private void myServerStop() { try { if (ChannelServices.GetChannel(“tcp”) != null) { ChannelServices.UnregisterChannel(tcpChannel); eventLog1.WriteEntry(“tắt server port ” + port.ToString() + ” lúc ” + DateTime.Now.ToString()); } } catch (Exception ex) { eventLog1.WriteEntry(“Lỗi : ” + ex.Message); } } protected override void OnStart(string[] args) { myServerStart(); } protected override void OnStop() { myServerStop(); } } } Như bạn hoàn thành xong class service MyPrimeServices8998 Tiếp theo ta tiến hành Add Installer cho Services, cách bấm chuột phải vào vùng xám, chọn Add Installer Chương trình tự động add thêm tập tin ProjectInstaller.cs, tập tin tự động chứa serviceInstaller1 serviceProcessInstaller1 hình bên dưới: Chỉnh sửa serviceProcessInstaller1: Sửa Account thành: LocalSystem Chỉnh sửa serviceInstaller1: Sửa ServiceName thành : MyPrimeServices8998 Sửa StartType thành: Automatic Sau chỉnh sửa xong, bạn tiến hành Build project MyPrimeServices Bước 2: Tạo project Setup cho bước Tương tự tầng khác, bạn tạo project chọn Setup Project mục Setup and Deployment/ Visual Studio Installer Đặt tên project : MyPrimeServicesSetup click OK Bạn quan sát Solution explorer, thấy tầng MyPrimeServicesSetup xuấn bên Để add Project mà bạn muốn cài đặt, bạn click chuột phải vào project MyPrimeService/ chọn Add/ chọn ProjectOutput Trong cửa sổ Add Project Output Group: bạn chọn MyPrimeServices Primary output nhấn nút OK Sau nhấn OK, bạn kết bên dưới: Tiếp tục, bấm chuột phải vào MyPrimeServiceSetup/ View/ Custom Actions Trong hình Design, bạn thấy bên dưới: Tiếp theo bạn bấm chuột phải vào Custom Actions/ chọn Add Custom Actions Màn hiình Select Item in Project hiển thị bên Tại hình này, bạn double click vào thư mục Application Folder nhấn OK: Sau nhấn OK, bạn thấy kết : Để hoàn tất trình tạo Project Setup, bạn bấm chuột phải vào MyPrimeServicesSetup, chọn Build/Rebuild: Sau Build thành công mục Install Uninstall cho phép bạn tương tác: Bạn chọn Install để tiến hành cài đặt chương trình tự tạo, qua bước để xem chi tiết: Bước 3: Cài đặt, chạy kiểm tra services, eventlog Sau chọn Install, hình cài đặt xuất bên dưới: Bấm Next để tiếp tục: Chờ chương trình cài đặt: Bạn cài đặt thành công MyPrimeService Bây để kiểm tra thực Service mà tạo tự động cài đặt hay chưa, bạn làm sau: Bấm chuột phải vào computer/ chọn Manage Nếu bạn thấy Service có tồn MyPrimeServices8998 hình thành công Để xem eventlog mà viết coding, bạn vào LOG8998 để quan sát: Như Service tự động cài đặt, bạn Start Stop Service tùy thích cách bấm chuột phải vào Service chọn Start Stop Bây cần chạy Client mà Thôi Server Service tự động start Bạn tiến hành cài đặt Service máy chủ cách lấy tập tin cài đặt tầng Setup cài vào máy chủ mong muốn, sau máy client bạn cần nhập cho IP máy chủ port 8998 việc OK Bạn thấy đấy, Tôi cần chạy Client… ... System.Text; using System.Windows.Forms; using System.Runtime .Remoting; using System.Runtime .Remoting. Channels; using System.Runtime .Remoting. Channels.Tcp; using ProxyObject; namespace ServerTier... Tương tự Add tham chiếu tới thư viện System.Runtime .Remoting, trường hợp bạn chọn tab NET thay tab Project Trong tab NET bạn chọn System.Runtime .Remoting nhấn OK Sau bạn quan sát tầng Server, hình... System.Text; using System.Windows.Forms; using System.Runtime .Remoting; using System.Runtime .Remoting. Channels; using System.Runtime .Remoting. Channels.Tcp; using ProxyObject; namespace TangServer

Ngày đăng: 15/08/2017, 19:45

Xem thêm: Activator trong .net remoting

TỪ KHÓA LIÊN QUAN

Mục lục

    Ví dụ mẫu .Net Remoting & Windows Services

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w