【问题标题】:Can't make MSDNs simple example c# service无法制作 MSDN 简单示例 c# 服务
【发布时间】:2017-03-02 13:42:50
【问题描述】:

我尝试按照 MSDN 简单的制作服务说明

演练:在组件设计器中创建 Windows 服务应用程序 https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {

        private System.ComponentModel.IContainer components;
        private System.Diagnostics.EventLog eventLog1;

        public Service1()
        {
            InitializeComponent();
            eventLog1 = new System.Diagnostics.EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart");
        }

        protected override void OnStop()
        {
        }


    }
}

我似乎在第二关“向服务中添加功能”失败了

编译时出现“WindowsService1.Service1.eventLog1”和“WindowsService1.Service1.Service1.eventLog1”之间的歧义,并且“WindowsService1.Service1”类型已经包含“组件”的定义

【问题讨论】:

  • 您是否已经在设计器文件中定义了componentseventLog1
  • 看来你得把变量叫做eventLog1
  • 看起来是这样,但我没有把它们放在那里。在我理解构造函数是什么之前只是盲目地遵循这些步骤等。似乎一旦我将事件日志添加到设计视图,它就会为组件和 eventlog1 创建两个条目,但这些步骤没有提到删除或编辑这些条目。

标签: c# service


【解决方案1】:

感谢 cmets 人让我找到了正确的位置,似乎您不需要执行第 4 步,它会在您在设计视图中添加事件日志时自动完成。

Service1.designer.cs looks like this

namespace WindowsService4
{
    partial class Service1
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.eventLog1 = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
            // 
            // Service1
            // 
            this.ServiceName = "Service1";
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

        }

        #endregion

        private System.Diagnostics.EventLog eventLog1;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多