C# 来自Windows服务的基类

C# 来自Windows服务的基类,c#,service,base-class,C#,Service,Base Class,我正在尝试为Windows服务创建基类。创建时,系统会自动创建: public partial class Service1 : ServiceBase { public class Base //added this to become a Base class { protected override void OnStart(string[] args)//generated code for Service {

我正在尝试为Windows服务创建基类。创建时,系统会自动创建:

     public partial class Service1 : ServiceBase
     {
     public class Base    //added this to become a Base class
     {
        protected override void OnStart(string[] args)//generated code for Service
        {
          //a bunch of code here that I create
         }
     }
     }
我想导出这个类:

     public class Derived : Base
      {

       void Call(string[] args)
         {
            Call test = new Call();
            test.OnStart(args);///error says no suitable method found to override
         }
      }
我之所以要这样做,是因为这项服务将与多种类型的数据库交互,我希望有尽可能多的代码可重用,每一个都将有相同的启动、停止等。。。我尝试在派生类中使用虚拟的、受保护的、公共的。我也不能更改生成的代码

如何调用受保护的覆盖启动?我最终也会有私人会员,因此我不必再问其他问题,如果在给那些人打电话时有什么我需要知道的,也会有帮助。

编辑后: 您必须从
ServiceBase
继承。仅仅在Service1的范围内创建公共类并不会创建继承。正确的定义是:

public class Derived : ServiceBase
{
    protected override void OnStart(string[] args)
    {
        //example
        int x = 1;

        //call the base OnStart with the arguments
        base.OnStart(args);
    }
}
然后,在程序类内部,您将创建这样的线束来运行它:

var servicesToRun = new[]
{
    new Derived()
};
ServiceBase.Run(servicesToRun);
MSDN参考


基于上述代码,受保护的OnStart方法需要参数
string[]args
。你需要传递一组参数。

你为什么要在
派生的
类中创建
Dervied
的实例,而不是访问
这个
实例?不,那只是一个大脑屁,谢谢,所以我很清楚你的答案,“派生的:ServiceBase”不是自动生成的,这就是我创造的,对吗?