C#服务开发和安装

时间:2018-05-24 20:19:20

标签: c# asp.net service

我按照本指南创建了Web服务: Windows Service Creation 但是还没有能够安装我的服务(仅代码就像Windows应用程序一样完美)

InstallUtil Error

我做错了什么?

英文错误: System.Security.SecurityException:找不到源,但无法搜索部分或全部事件日志。无法访问的日志:安全性

服务代码:

namespace EmailDoc2
{
    public partial class Emailing : ServiceBase
    {
        private Timer time = null;

        public Emailing()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            time = new Timer();
            this.time.Interval = 43200000;
            this.time.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_tick);
            time.Enabled = true;
            using (StreamWriter w = File.AppendText("Log Boletas/logBoletas.txt"))
            EmailDoc.EmailDoc.Log("Service Started", w);
        }
        private void timer_tick(object sender, EventArgs e)
        {
            EmailDoc.EmailDoc.Main();
        }

        protected override void OnStop()
        {
            time.Enabled = false;
            using (StreamWriter w = File.AppendText("Log Boletas/logBoletas.txt"))
            EmailDoc.EmailDoc.Log("Service Stopped", w);
        }
    }

1 个答案:

答案 0 :(得分:0)

这是一个新手的错误,但最后,页面根本没有错,我遇到的问题是我删除了启动Windows服务的部分代码:

namespace EnvioDocumentos
{
    static class Program
    {
        /// <summary>
        /// Code bellow is default, never erase
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new EnvioDocs() 
            };
            ServiceBase.Run(ServicesToRun);
       }
    }
}

对于像我这样的新手,我强烈建议你创建一个新的课程(Library.cs)来调用你的方法,这样你就不会被困在&#34;我不知道这项服务如何运作&#34;

        protected override void OnStart(string[] args)
        {
        timer1 = new Timer();
        this.timer1.Interval = yourtime;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timertick); //<== This function starts every "yourtime" in milliseconds
        timer1.Enabled = true;
        Library.WriteErrorLog("Test start"); //<== best practice ever to write a log every beginning
        }

https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#BK_CreateProject