Web启动,停止Windows服务

When you grow stronger,the world become more dangerous.当你变得越强大,这个世界反而会变得越危险。

ServiceModel.cs代码:

  public class ServiceModel
    {
        public string ServiceName { get; set; }

        public string DisplayName { get; set; }

        public bool IsRunning { get; set; }
    }

wServiceHandler.ashx代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Web;

namespace wServiceManager
{
    /// <summary>
    /// wServiceHandler 的摘要说明
    /// </summary>
    public class wServiceHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //服务名
            string serviceName = context.Request["serviceName"];
            //操作类型【重启、停止、重启】
            string type = context.Request["type"];

            try
            {
                switch (type)
                {
                    case "start":
                        StartService(serviceName);
                        break;
                    case "stop":
                        StopService(serviceName);
                        break;
                    case "reset":
                        ResetService(serviceName);
                        break;
                    default:
                        ResetService(serviceName);
                        break;
                }

                context.Response.Write("ok");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        private void StartService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            if (service.Status == ServiceControllerStatus.Stopped)
            {
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
                service.Close();
            }
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        private void StopService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                service.Close();
            }
        }

        /// <summary>
        /// 重启服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        private void ResetService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            if (service.Status == ServiceControllerStatus.Running || service.Status == ServiceControllerStatus.Stopped)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
                service.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

IndexManager.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IndexManager.aspx.cs" Inherits="wServiceManager.IndexManager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css" />
        <script src="js/jquery1.12.4.js" type="text/javascript"></script>
        <script src="js/bootstrap.js" type="text/javascript"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="container-fluid">
                <div class="row-fluid">
                    <h3>
                        Windows服务管理
                    </h3>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>
                                    服务标识的名称
                                </th>
                                <th>
                                    服务的友好名称
                                </th>
                                <th>
                                    状态
                                </th>
                                <th>
                                    操作
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <%
                                int count = list.Count;
                                for (int i = 0; i < count; i++)
                                {
                                    string dname = list[i].DisplayName.Trim();
                                    string sname = list[i].ServiceName.Trim();
                                    string isRun = list[i].IsRunning ? "运行中" : "停止中";
                            %>
                                <tr>
                                    <td>
                                        <%= dname %>
                                    </td>
                                    <td id="sname">
                                        <%= sname %>
                                    </td>
                                    <td>
                                        <%= isRun %>
                                    </td>
                                    <td>
                                        <% if (list[i].IsRunning)
                                           { %>
                                            <button class="btn btn-danger" id="stopService" type="button">
                                                停止</button>
                                        <%
                                           }
                                           else
                                           { %>
                                            <button class="btn btn-success" id="startService" type="button">
                                                启动</button>
                                        <% } %>
                                    </td>
                                </tr>
                            <% } %>
                        </tbody>
                    </table>
                </div>
            </div>
        </form>
        <script type="text/javascript">
            var sname = $("#sname").text().trim();
            $("#startService").click(function() {
                $.ajax({
                    type: "post",
                    url: "wServiceHandler.ashx",
                    data: { "serviceName": sname, "type": "start" },
                    success: function(result) {
                        if (result == "ok") {
                            window.location.reload();
                        }

                    }
                });
            });
            $("#stopService").click(function() {
                $.ajax({
                    type: "post",
                    url: "wServiceHandler.ashx",
                    data: { "serviceName": sname, "type": "stop" },
                    success: function(result) {
                        if (result == "ok") {
                            window.location.reload();
                        }
                    }
                });
            });
        </script>
    </body>
</html>

IndexManager.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceProcess;

namespace wServiceManager
{
    public partial class IndexManager : System.Web.UI.Page
    {
        public List<ServiceModel> list=new List<ServiceModel>();
        protected void Page_Load(object sender, EventArgs e)
        {
            ServiceController[] myServices = ServiceController.GetServices();

            list = new List<ServiceModel>();
            foreach (var item in myServices)
            {
                if (item.ServiceType == ServiceType.Win32OwnProcess && item.DisplayName.Contains("memcached"))
                {
                    ServiceModel model = new ServiceModel();
                    model.ServiceName = item.ServiceName;
                    model.DisplayName = item.DisplayName;
                    if(item.Status == ServiceControllerStatus.Running)
                    {
                        model.IsRunning = true;
                    }
                    else
                    {
                        model.IsRunning = false;
                    }
                    list.Add(model);
                }
            }
        }
    }
}

运行结果如图:

这里写图片描述


这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Sybase ASE 15.7 开发文档:Web 服务用户指南 第 1 部分 简介 第 1 章 了解 Adaptive Server Enterprise Web 服务 .......... 3 概述 .......... 3 Adaptive Server Enterprise Web 服务 .......... 4 ASE Web 服务的优点 .......... 4 存储过程和函数 .......... 4 SQL .......... 4 安全性 .......... 5 LDAP .......... 5 用户定义的 Web 服务 .......... 5 Web 服务标准 .......... 5 XML .......... 6 WSDL .......... 9 SOAP .......... 10 第 2 章 了解 ASE Web 服务引擎 .......... 13 Web 服务的生产者 .......... 13 生产者组件 .......... 14 生产者 Web 方法 .......... 15 用户定义的 Web 服务 .......... 15 Web 服务的使用者 .......... 16 使用者组件 .......... 17 代理表 .......... 18 第 2 部分 配置 第 3 章 配置 ASE Web 服务 .......... 21 配置 .......... 21 在安装期间进行配置 .......... 22 在安装完成后进行配置 .......... 22 许可 .......... 23 配置文件 .......... 23 ws.properties 文件 .......... 23 logging.properties 文件 .......... 23 wsmsg.properties 文件 .......... 24 安全性 .......... 24 配置 SSL .......... 24 为 Microsoft .NET 安装证书 .......... 26 第 3 部分 管理 第 4 章 使用 ASE Web 服务 .......... 29 使用 ASE Web 服务引擎 .......... 29 启动停止 ASE Web 服务引擎 .......... 29 ASE Web 服务方法 .......... 31 使用 sp_webservices .......... 35 调用 Web 服务 .......... 38 使用用户定义的 Web 服务 .......... 40 用户定义的 Web 服务的命令 .......... 41 将 sp_webservices 与用户定义的 Web 服务一起使用 .......... 45 用户定义的 Web 服务的安全性 .......... 47 用户定义的 Web 服务的审计 .......... 48 ASE Web 服务日志记录 .......... 49 ASE Web 服务日志文件 .......... 49 转滚日志文件 .......... 50 使用 Sybase Central .......... 50 第 5 章 示例应用程序 .......... 51 Apache 示例客户端 .......... 51 创建示例客户端 .......... 51 使用 runexecute .......... 52 Microsoft .NET 示例客户端 .......... 55 创建示例客户端 .......... 56 使用 Execute.exe .......... 56 第 4 部分 故障排除 第 6 章 故障排除 .......... 61 故障排除问题 .......... 61 远程服务器类定义设置 .......... 61 未映射 RPC/ 编码 Web 方法 .......... 62 文档 / 文字结果被截断 .......... 62 启动 ASE Web 服务引擎 .......... 63 定位 WSDL .......... 63 指定 ws.properties 中的条目 .......... 63 Windows NT 命令行参数 .......... 64 运行或停止脚本失败 .......... 64 空口令 .......... 64 指定使用 SSL 的 SOAP 结束点 .......... 65 sp_webservices ‘add’ 异常终止 .......... 65 Web 服务代理表限制 .......... 65 sysattributes 表条目 .......... 66 诊断工具 .......... 66 详细记录

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值