.NET Remoting 入門實例

1.建立服務端Class:ProxyServerRemoting服務器

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using Inscription.Manadal.EmrPlugIn.NetMessage;
 5 using NLog;
 6 using Inscription.Manadal.EmrPlugIn;
 7 using System.ComponentModel;
 8 using System.Runtime.Remoting;
 9 using System.Runtime.Remoting.Channels.Tcp;
10 using System.Runtime.Remoting.Channels;
11 
12 namespace Inscription.Mandala.EmrPlugIn.ProxyServer
13 {
14     public class ProxyServerRemoting
15     {
16         // 監聽端口
17         private static int port;
18         // 單例對象
19         private static ProxyServerRemoting Instance = null;
20         // 後臺工做線程對象
21         private BackgroundWorker backWork = null;
22         //定義服務端監聽信道
23         private static TcpServerChannel tcpServerChannel = null; 
24 
25         private ProxyServerRemoting()
26         {
27             //建立後臺工做對象(線程)
28             backWork = new BackgroundWorker();
29             //綁定DoWork事件程序
30             backWork.DoWork += new DoWorkEventHandler(backWork_DoWork);
31             //開始執行後臺操做
32             backWork.RunWorkerAsync();
33         }
34 
35         /// <summary>
36         /// 後臺線程
37         /// </summary>
38         /// <param name="sender"></param>
39         /// <param name="e"></param>
40         private void backWork_DoWork(object sender, DoWorkEventArgs e)
41         {
42             StartServer();
43         }
44 
45         /// <summary>
46         /// 單例實現
47         /// </summary>
48         /// <returns></returns>
49         public static ProxyServerRemoting getInstance(int Port)
50         {
51             if (Instance == null)
52             {
53                 Instance = new ProxyServerRemoting();
54                 tcpServerChannel = new TcpServerChannel(port);
55                 port = Port;
56             }
57             return Instance;
58         }
59 
60         /// <summary>
61         /// 啓動服務
62         /// </summary>
63         public void StartServer()
64         {
65             ChannelServices.RegisterChannel(tcpServerChannel, false);
66                         
67             RemotingConfiguration.RegisterWellKnownServiceType(typeof(epiManagerV2), "GetEmrPlugInFunctionV3", WellKnownObjectMode.Singleton);
68         }
69 
70         /// <summary>
71         /// 中止服務
72         /// </summary>
73         public void StopServer()
74         {
75             ChannelServices.UnregisterChannel(tcpServerChannel);
76         }
77     }
78 }

2.建立客戶端調用Class:ProxyClienttcp

客戶端每次調用完成之後,須要註銷掉當前信道,ChannelServices.UnregisterChannel(tcpChannel);ide

否則會發生異常:信道tcp已註冊函數

using System;
using System.Collections.Generic;
using System.Text;
using NLog;
using System.Runtime.Remoting.Channels.Tcp;
using System.Diagnostics;
using System.Configuration;
using System.Reflection;
using System.Runtime.Remoting.Channels;
using Inscription.Manadal.EmrPlugIn;

namespace Inscription.Mandala.EmrPlugIn.ProxyClient
{
    public static class ProxyClient
    {
        /// <summary>
        /// 獲取基於函數的外部接口
        /// </summary>
        /// <param name="MainName"></param>
        /// <param name="ConfigInfo"></param>
        /// <param name="worker"></param>
        /// <param name="patientIndex"></param>
        /// <returns></returns>
        public static string GetEmrPlugInFunctionV3(string MainName, string ConfigInfo, string strWorker, string strPatientIndex, string CommandKey, string strParamLst)
        {
            TcpChannel tcpChannel = null;
            Logger logger = NLogManagerV2.GetLogger("GetEmrPlugInFunctionV3_Client");
            try
            {
                logger.Trace("判斷服務端進程是否存在");

                string strAppName = "IMPIProxyServer";
                Process[] curProcesses = Process.GetProcesses();
                bool isExist = false;
                foreach (Process p in curProcesses)
                {
                    if (p.ProcessName == strAppName)
                    {
                        isExist = true;
                        logger.Trace("服務端進程存在");
                        break;
                    }
                }
                if (isExist == false)
                {
                    logger.Trace("服務端進程不存在");
                    Process.Start(strAppName);
                    logger.Trace("從新啓動服務端進程");
                }

                //int port = 3399;
                string ip = ConfigurationManager.AppSettings["ServerIP"];
                int port = Convert.ToInt32(ConfigurationManager.AppSettings["ServerPort"]);
                logger.Trace("監聽IP:" + ip + "    監聽端口" + port);


                //使用TCP通道獲得遠程對象
                tcpChannel = new TcpChannel();
                ChannelServices.RegisterChannel(tcpChannel, false);
                IepiManagerV2 manager = (IepiManagerV2)Activator.GetObject(typeof(IepiManagerV2), string.Format("tcp://{0}:{1}/GetEmrPlugInFunctionV3", ip, port));
                logger.Trace("取得.Net Remoting對象成功");

                string strReturn = manager.GetEmrPlugInFunctionV3(MainName, ConfigInfo, strWorker, strPatientIndex, CommandKey, strParamLst);
                logger.Trace("客戶端調用結束");
                return strReturn;
            }
            catch (Exception ex)
            {
                logger.Trace(ex.Message);
                return string.Empty;
            }
            finally
            {
                ChannelServices.UnregisterChannel(tcpChannel);
            }
        }
    }
}

 

3.業務類:epiManagerV2 工具

業務類實現了IepiManagerV2接口,其中客戶端也連接引用了IepiManagerV2接口文件,這樣客戶端就擺脫了對具體業務Dll的依賴,this

客戶端編譯成一個單獨的dll可被第三方程序調用。spa

public class epiManagerV2 :  MarshalByRefObject,IepiManagerV2
{
        /// <summary>
        /// 獲取基於函數的外部接口
        /// </summary>
        /// <param name="MainName"></param>
        /// <param name="ConfigInfo"></param>
        /// <param name="worker"></param>
        /// <param name="patientIndex"></param>
        /// <returns></returns>
        public string GetEmrPlugInFunctionV3(string MainName, string ConfigInfo, string strWorker, string strPatientIndex, string CommandKey, string strParamLst)
        {
        實現略
        }            
}

 IepiManagerV2接口線程

using System;
namespace Inscription.Manadal.EmrPlugIn
{
    interface IepiManagerV2
    {
        string GetEmrPlugInFunctionV3(string MainName, string ConfigInfo, string strWorker, string strPatientIndex, string CommandKey, string strParamLst);
    }
}

 4.服務端管理工具 code

實現啓動服務,暫停服務,開機自動運行功能orm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Configuration;
using Microsoft.Win32;
using System.IO;

namespace Inscription.Mandala.EmrPlugIn.ProxyServer
{
    public partial class frmMainServer : Form
    {
        private ProxyServerRemoting Instance = null;

        string ip = "127.0.0.1";
        int port = 3399;

        public frmMainServer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            InitForm();
            StartServer();
        }

        private void InitForm()
        {
            this.ip = ConfigurationManager.AppSettings["ServerIp"];
            this.port = Convert.ToInt32(ConfigurationManager.AppSettings["ServerPort"]);
        }

        /// <summary>
        /// 打開服務端
        /// </summary>
        private void StartServer()
        {
            try
            {
                Instance = ProxyServerRemoting.getInstance(port);
                Instance.StartServer();

                btnStart.Enabled = false;
                btnStop.Enabled = true;
                lblStatus.Text = "服務正在運行";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 關閉服務
        /// </summary>
        private void StopServer()
        {
            try
            {
                Instance.StopServer();

                btnStart.Enabled = true;
                btnStop.Enabled = false;
                lblStatus.Text = "服務已關閉";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 窗口顯示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMainServer_Shown(object sender, EventArgs e)
        {
            this.Hide();
        }

        /// <summary>
        /// 啓動
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, System.EventArgs e)
        {
            StartServer();
        }

        /// <summary>
        /// 暫停服務器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbStop_Click(object sender, EventArgs e)
        {
            StopServer();
        }

        /// <summary>
        /// 關閉
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMainServer_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Instance.StopServer();
        }

        /// <summary>
        /// 菜單命令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "打開":
                    this.Show();
                    break;
                case "隱藏":
                    this.Hide();
                    break;
                case "關閉":
                    Instance.StopServer();
                    this.Close();
                    break;
            }
        }

        /// <summary>
        /// 工具欄事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "tsbAutoRun":
                    SetAutoRun(Core.App.fullPath, true);
                    break;
                case "tsbAutoRunCancel":
                    SetAutoRun(Core.App.fullPath, false);
                    break;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }


        /// <summary>
        /// 設置應用程序開機自動運行
        /// </summary>
        /// <param name="fileName">應用程序的文件名</param>
        /// <param name="isAutoRun">是否自動運行,爲false時,取消自動運行</param>
        /// <exception cref="System.Exception">設置不成功時拋出異常</exception>
        private static void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }
        }


        public static void Test()
        {
            frmMainServer f = new frmMainServer();
            f.Show();
        }
    }
}
相關文章
相關標籤/搜索