.NET Remoting 入门实例
1.创建服务端Class:ProxyServerRemoting
using System;
using System.Collections.Generic;
using System.Text;
using Inscription.Manadal.EmrPlugIn.NetMessage;
using NLog;
using Inscription.Manadal.EmrPlugIn;
using System.ComponentModel;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels; namespace Inscription.Mandala.EmrPlugIn.ProxyServer
{
public class ProxyServerRemoting
{
// 监听端口
private static int port;
// 单例对象
private static ProxyServerRemoting Instance = null;
// 后台工作线程对象
private BackgroundWorker backWork = null;
//定义服务端监听信道
private static TcpServerChannel tcpServerChannel = null; private ProxyServerRemoting()
{
//创建后台工作对象(线程)
backWork = new BackgroundWorker();
//绑定DoWork事件程序
backWork.DoWork += new DoWorkEventHandler(backWork_DoWork);
//开始执行后台操作
backWork.RunWorkerAsync();
} /// <summary>
/// 后台线程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backWork_DoWork(object sender, DoWorkEventArgs e)
{
StartServer();
} /// <summary>
/// 单例实现
/// </summary>
/// <returns></returns>
public static ProxyServerRemoting getInstance(int Port)
{
if (Instance == null)
{
Instance = new ProxyServerRemoting();
tcpServerChannel = new TcpServerChannel(port);
port = Port;
}
return Instance;
} /// <summary>
/// 启动服务
/// </summary>
public void StartServer()
{
ChannelServices.RegisterChannel(tcpServerChannel, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(epiManagerV2), "GetEmrPlugInFunctionV3", WellKnownObjectMode.Singleton);
} /// <summary>
/// 停止服务
/// </summary>
public void StopServer()
{
ChannelServices.UnregisterChannel(tcpServerChannel);
}
}
}
2.创建客户端调用Class:ProxyClient
客户端每次调用完成以后,需要注销掉当前信道,ChannelServices.UnregisterChannel(tcpChannel);
不然会发生异常:信道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的依赖,
客户端编译成一个单独的dll可被第三方程序调用。
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.服务端管理工具
实现启动服务,暂停服务,开机自动运行功能
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 = ; 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(@"\") + );
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();
}
}
}
.NET Remoting 入门实例的更多相关文章
- React 入门实例教程(转载)
本人转载自: React 入门实例教程
- struts入门实例
入门实例 1 .下载struts-2.3.16.3-all .不摆了.看哈就会下载了. 2 . 解压 后 找到 apps 文件夹. 3. 打开后将 struts2-blank.war ...
- Vue.js2.0从入门到放弃---入门实例
最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...
- wxPython中文教程入门实例
这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下 wxPython中文教程入门实例 wx.Window 是一个基类 ...
- Omnet++ 4.0 入门实例教程
http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...
- Spring中IoC的入门实例
Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...
- Node.js入门实例程序
在使用Node.js创建实际“Hello, World!”应用程序之前,让我们看看Node.js的应用程序的部分.Node.js应用程序由以下三个重要组成部分: 导入需要模块: 我们使用require ...
- Java AIO 入门实例(转)
Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...
- Akka入门实例
Akka入门实例 Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用. Actor模型并非什么新鲜事物,它由Carl Hew ...
随机推荐
- 高效的 itertools 模块(转)
原文地址:http://python.jobbole.com/87380/ 我们知道,迭代器的特点是:惰性求值(Lazy evaluation),即只有当迭代至某个值时,它才会被计算,这个特点使得迭代 ...
- Electron与jQuery中$符号冲突的三种解决方法
在Electron工程中引用jQuery时,经常会出现以下错误: Uncaught ReferenceError: $ is not defined 解决的具体方法如下: ①.在测试的过程中(测试过1 ...
- 【找规律】【DFS】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
假设一个数有n个质因子a1,a2,..,an,那么n'=Σ(a1*a2*...*an)/ai. 打个表出来,发现一个数x,如果x'=Kx,那么x一定由K个“基础因子”组成. 这些基础因子是2^2,3^ ...
- 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market
傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...
- Problem A: 指针:调用自定义交换函数,完成三个数整从小到大排列
#include<stdio.h> int swap(int *p1,int *p2) { int flag; if(*p1>*p2) { flag=*p1; *p1=*p2; *p ...
- ConcurrentHashMap(Java8)源码分析
1. 常量.成员变量 private static final int MAXIMUM_CAPACITY = 1 << 30; // 和HashMap一样 private static f ...
- js 封装的自动创建表格的相关操作
因为要做一个动态输入的表格,现在积累一下资料,在网上找了一些资料,经过总结是使用更加方便些,谁有更好的插件和封装的东西,请大家分享一下. <script type="text/java ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- web安全开发指南--认证
1.认证 1.1. 认证和密码管理安全规则 1 认证控制必须只能在服务器端执行. 2 除了指定为公开的资源,对所有其它资源的访问都必须先经过认证. 3 为所有关键凭证实施防"暴力 ...
- <摘录>cocos2d-x 从环境搭建到win32项目移植android平台
软件:cocos2d-x-2.2.3:android-ndk-r9d:adt-bundle-windows-x86_64-20131030:python-2.7.6: 1安装配置python 安装没什 ...