net下的windows服务

部署.net下的windows服务
创建windows服务
(1)、建立一个新的windows服务项目Server1 
(2)、打开Service1代码视图,找到OnStart部分,加入代码 
(3)、切换到设计视图,右键-添加安装程序 
(4)、切换到新生成的ProjectInstaller.cs设计视图,找到serviceProcessInstaller1对Account属性设置为LocalSystem,对serviceInstaller1的ServiceName属性设置为Server1(服务的名字),StartType属性设置为Automatic(系统启动的时候自动启动服务) 
(5)、关闭刚才的项目。
(6)、建立一个新的安装项目ServerSetup(我们为刚才那个服务建立一个安装项目) 
(7)、右键-添加-项目输出-主输出-选择Service1-确定 
(8)、右键-视图-自定义操作-自定义操作上右键-添加自定义操作-打开应用程序文件夹-选择刚才那个主输出-确定 
(9)、重新生成这个安装项目-右键-安装 
(10)、在服务管理器中(我的电脑-右键-管理-服务和应用程序-服务)找到Server1服务,启动服务
 
安装或卸载(服务器上)
(1)将按照包setup文件直接拷贝到服务器上。进行安装,如果已经安装,则会让你卸载后在安装。
 
 
调试代码例子
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.Net;
using System.IO;
using System.Threading;

namespace WindonTest
{
    
    public partial class Service1 : ServiceBase
    {
  
        public Service1()
        {
            InitializeComponent();
        }

        //启动服务时执行
        protected override void OnStart(string[] args)
        {
            Thread.Sleep(3000);  //调试前需要进程休眠几分钟
            System.Diagnostics.Debugger.Launch();
      
            // TODO: 在此处添加代码以启动服务。
            //使时间控件生效 并且1秒钟调用一次timer1_Elapsed() 方法
            //log.Error("服务已经启用");


            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start1111116."); 
            }
       
            System.Timers.Timer time = new System.Timers.Timer();
            time.Interval = 1000;
            time.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
            time.AutoReset = true;
            time.Enabled = true;
        
        }
        //停止服务时执行
        protected override void OnStop()
        {
          
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\"+DateTime.Now.ToString("yyyy-MM-dd")+".txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop222226.");
            }
        }
        //暂停服务时执行
        protected override void OnPause()
        {
        }
        //继续服务时执行
        protected override void OnContinue()
        {
        }
        //根据地址获得一个页面的源码
        public string GetHttpData(string Url)
        {
            string sException = null;
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(Url);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }
            catch (WebException e)
            {
                sException = e.Message.ToString();
                //Response.Write(sException);
            }
            catch (Exception e)
            {
                sException = e.ToString();
                //Response.Write(sException);
            }
            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "服务已经启用,在timer1_Tick执行中,已经修改了");
            }
            GetHttpData("http://container.api.taobao.com/container?appkey=21723715");
            GetHttpData("http://www.inshion.com/?top_appkey=21723715&top_parameters=ZXhwaXJlc19pbj04NjQwMCZpZnJhbWU9MSZyMV9leHBpcmVzX2luPTg2NDAwJnIyX2V4cGlyZXNfaW49ODY0MDAmcmVfZXhwaXJlc19pbj04NjQwMCZyZWZyZXNoX3Rva2VuPTYxMDAwMDkyYmI1NDA4ZTYxMTQxYTY0ZGE5ODlhYjgxNjg4YTIyMjliN2I3ZGM5MTAwMjQ0Nzg4JnRzPTEzOTAxODY4ODM3MTgmdmlzaXRvcl9pZD0xMDAyNDQ3ODgmdmlzaXRvcl9uaWNrPdTayMjE1tbQvMXEryZ3MV9leHBpcmVzX2luPTg2NDAwJncyX2V4cGlyZXNfaW49ODY0MDA%3D&top_session=6100a09df8507a60d142f3f53697d79f49872e08692bfea100244788&qq-pf-to=pcqq.c2c&agreement=true&agreementsign=21723715-23169781-100244788-2B8DDA1A3245F94933C60BE4BFEAB5FC&top_sign=i%2F1N%2B7vHFRHaaNQZbvBG0A%3D%3D");
            //this.timer1.Enabled = false;
            ////在此添加您们需要的功能代码, 我在此添加检测计划是否需要执行
            //this.timer1.Enabled = true;
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "服务已经启用,在timer1_Tick执行中");
            }
        }
    }
}

 

posted on 2014-01-24 10:08  闪电光芒  阅读(185)  评论(0)    收藏  举报

导航