Reputation: 109
I'm trying to create a base class for a Windows Service. When creating, this is created automatically:
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
}
}
}
And I Want to derive this class :
public class Derived : Base
{
void Call(string[] args)
{
Call test = new Call();
test.OnStart(args);///error says no suitable method found to override
}
}
The reason I want to do this is because this service will interact with multiple types of Databases and I want to have as much code reusable as possible, each one will have the same OnStart, OnStop etc... I've tried to use virtual, protected, public in the derived class. I can't change the generated code also.
How can I call the protected override OnStart? I will also have private members eventually and so I don't have to ask another question, if theres anything I need to know when calling those that would be helpful too..
Upvotes: 1
Views: 4321
Reputation: 6554
After your edit:
You have to inherit from ServiceBase
. Simply creating a public class inside the scope of Service1 does not create inheritance. The correct definition is:
public class Derived : ServiceBase
{
protected override void OnStart(string[] args)
{
//example
int x = 1;
//call the base OnStart with the arguments
base.OnStart(args);
}
}
Then, inside of your Program class, you would create such harness to run it:
var servicesToRun = new[]
{
new Derived()
};
ServiceBase.Run(servicesToRun);
MSDN Reference here
The protected OnStart method requires the argument string[] args
, based on your code above. You need to pass an array of arguments.
Upvotes: 2