-1

I need to pass parameters to Main(string[] args) of windows service, I'm able to pass parameters to void OnStart(string[] args) through the properties of service by using

How to pass parameters to Windows Service?

sample code:

static void Main(string[] args)////How to pass args to this function
{
//// some code
}
protected override void OnStart(string[] args)////by using above link i can pass parameters to this function
{
////some code
}

Note : I used project properties to pass the args to main() but it can't help in real time.

Is this a right way to do it because I can't find any information regarding this?

Edit 1:

I refered this How to pass parameters in windows service from Installer to Main function in Program.cs? but i didnt get much information why we cant pass?

Edit 2:

My goal is to pass a Raygun key to Main(String[] args) then log the errors to the Raygun Website,so based on the key it will log errors to the particular version. So i need to pass Raygun key dynamically to the service, i can pass the key through the Service properties, then it will log only onstart(...) level so if any exception are handled at main() level i can't able to log.So i need to initialize the below code at main method level or while Service Starts

_raygunClient = new RaygunClient("Dynamic_Key");

service will start manually at any time for development prospective.

Community
  • 1
  • 1
Ajay
  • 2,022
  • 19
  • 33
  • You can't, not without altering the image path of the service. The executable is started first (`main()`), then the service control manager requests service start (`OnStart()`). Why do you want to use `main()`, and why don't you simply change the image path if you want that? – CodeCaster May 25 '15 at 09:20
  • possible duplicate of [How to pass parameters in windows service from Installer to Main function in Program.cs?](http://stackoverflow.com/questions/5055730/how-to-pass-parameters-in-windows-service-from-installer-to-main-function-in-pro) – Tony Hopkinson May 25 '15 at 09:22
  • Why doesn't the linked duplicate answer your question? – usr May 25 '15 at 09:46
  • That answer mainly concentrate on how to pass parameters to onstart() Method.The only point which is helpful for me, from that answer is "Process initialization arguments for the service in the OnStart method, not in the Main method", So there is no alternative for this type of cases? – Ajay May 25 '15 at 10:01
  • @CodeCaster Currently im working in support project,so i dont want to change existing code like pass parameter through service properties because they already passing some parameters from years ago if i change now it will be create some additional work. – Ajay May 25 '15 at 10:05
  • @Ajay I'm not sure what question you're answering, but you can (and have to) change the service's image path if you want to pass additional parameters to `main()`. – CodeCaster May 25 '15 at 10:12
  • May i know why it is Downvoted?I followed all the stackoverflow rules. – Ajay May 25 '15 at 11:43
  • What are you trying to achieve? Who will be sending those parameters and at what time? Will you dynamically start the service and try to pass parameters? – usr May 25 '15 at 11:56
  • @usr please check my edit2, if you need further info you can drop a comment – Ajay May 25 '15 at 13:40

1 Answers1

1

Pass the Raygun key as commandline arguments. Set the key at installation time. There is no need for dynamic arguments.

Community
  • 1
  • 1
usr
  • 168,620
  • 35
  • 240
  • 369