WCF 服务管理
2024-09-29 15:18:25
public class ContractManager
{ IDictionary stateSaver = new Hashtable();
/// <summary>
/// 安装服务
/// </summary>
/// <param name="p_Path">指定服务文件路径</param>
/// <param name="p_ServiceName">返回安装完成后的服务名</param>
/// <returns>安装信息 正确安装返回""</returns>
public static string InsertService(string p_Path, ref string p_ServiceName)
{
if (!File.Exists(p_Path)) return "文件不存在!";
p_ServiceName = "";
FileInfo _InsertFile = new FileInfo(p_Path);
IDictionary _SavedState = new Hashtable();
try
{
//加载一个程序集,并运行其中的所有安装程序。
AssemblyInstaller _AssemblyInstaller = new AssemblyInstaller(p_Path, new string[] { "/LogFile=" + _InsertFile.DirectoryName + "\\" + _InsertFile.Name.Substring(, _InsertFile.Name.Length - _InsertFile.Extension.Length) + ".log" });
_AssemblyInstaller.UseNewContext = true;
_AssemblyInstaller.Install(_SavedState);
_AssemblyInstaller.Commit(_SavedState);
Type[] _TypeList = _AssemblyInstaller.Assembly.GetTypes();//获取安装程序集类型集合
for (int i = ; i != _TypeList.Length; i++)
{
if (_TypeList[i].BaseType.FullName == "System.Configuration.Install.Installer")
{
//找到System.Configuration.Install.Installer 类型
object _InsertObject = System.Activator.CreateInstance(_TypeList[i]);//创建类型实列
FieldInfo[] _FieldList = _TypeList[i].GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
for (int z = ; z != _FieldList.Length; z++)
{
if (_FieldList[z].FieldType.FullName == "System.ServiceProcess.ServiceInstaller")
{
object _ServiceInsert = _FieldList[z].GetValue(_InsertObject);
if (_ServiceInsert != null)
{
p_ServiceName = ((ServiceInstaller)_ServiceInsert).ServiceName;
return "";
}
}
}
}
}
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="filepath">服务路径</param>
public static void UnInstallService(string filepath, string ServiceName)
{ try
{ if (ServiceIsExisted(ServiceName))
{ //UnInstall Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } } catch (Exception ex)
{
throw new Exception("unInstallServiceError\n" + ex.Message); } }
/// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static bool ServiceStart(string serviceName)
{
try
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Running)
{
return true;
}
else
{
TimeSpan timeout = TimeSpan.FromMilliseconds( * );
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch
{
return false;
}
return true;
} /// <summary>
/// 停止服务
/// </summary>
/// <param name="serviseName"></param>
/// <returns></returns>
public static bool ServiceStop(string serviseName)
{
try
{
ServiceController service = new ServiceController(serviseName);
if (service.Status == ServiceControllerStatus.Stopped)
{
return true;
}
else
{
TimeSpan timeout = TimeSpan.FromMilliseconds( * );
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch
{
return false;
}
return true;
} }
最新文章
- 救援linux
- IIS 服务没有及时响应启动或控制请求
- Maven使用笔记,错误Failure to Transfer后处理
- shell学习笔记(2)替换命令&#183;&#183;与()的区别
- [CODEVS1697]⑨要写信
- JQuery 解决 鼠标快速滑过后,会执行多次滑出的问题
- HDU 3362 Fix
- [置顶] 吃论扯谈---吃货和Office 365订阅的关系
- [Android]获取设备相关信息
- NET 分布式架构开发项目实战
- 201621123062《java程序设计》第五周作业总结
- echarts图表
- jQuery Validate和Thymeleaf相关
- Kaazing Gateway简单使用
- python实现关键词提取
- 000 Python的运行
- Maven依赖标红线,非jar包冲突问题
- sudo权限的设置
- .net图表之ECharts随笔05-不同01的语法步骤
- adb remount 失败:remount failed: Operation not permitted