none
Call my method in c# windows service

    Question

  • Hi every body. I wrote a windows service with c# in VS 2008. It had OnStart(string[] args) and OnStop() methods. I added my BackupDatabase() method to that.

    Now I can call OnStart(string[] args) method by ServiceController.Start().

    But how can I call BackupDatabase() method?


    Each problem has a solution.
    Thursday, October 21, 2010 6:09 PM

Answers

  • Hello,


    There is a very easy way of sending custom command to a WebService described in this article .


    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by mammadkoma Friday, October 22, 2010 2:56 AM
    Thursday, October 21, 2010 6:14 PM
  • Hello,

     

    You cannot do this in a windows service (especially the part with returning a value.

    Other services are also not receiving messages, that's why after some changes to the SQL Server, you have to restart the service.

    Wcf will do all of these and more, and once you get the hang of it you will see that it is actually really easy to create and use.

    The links i've provided earlier should get you started if you decide to use it, if you need any help, please let me know.

     

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by mammadkoma Friday, October 22, 2010 5:55 PM
    Friday, October 22, 2010 5:48 PM
  • A cool example is here:

    http://www.codeproject.com/KB/WCF/WCF_windows_hosting.aspx


    Each problem has a solution.
    • Marked as answer by mammadkoma Saturday, October 23, 2010 6:27 AM
    Saturday, October 23, 2010 6:26 AM

All replies

  • Hello,


    There is a very easy way of sending custom command to a WebService described in this article .


    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by mammadkoma Friday, October 22, 2010 2:56 AM
    Thursday, October 21, 2010 6:14 PM
  • Hello,


    There is a very easy way of sending custom command to a WebService described in this article .


    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thanks for reply Emanuel, Varga

    My methods in windows service get varible arguments. 

    How can I pass their arguments?


    Each problem has a solution.
    Friday, October 22, 2010 3:36 AM
  • does it not call your backupdatabase method???

    the onstart method is a virtual method. it is called automatically when the service is called

    see: http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx

    btw. .  if you want to debug your service when running,

    Add this code to your OnStart:

    System.Diagnostics.Debugger.Launch();

    and then install your service

     

     


    gimme some slamming techno!!!!
    Friday, October 22, 2010 3:53 AM
  • Hello again,

     

    What type of parameters do you need to send to the service?

    Like i said, the only option is that with the custom command, if you can map your commands, let's say as an enumeration, than this will work perfectly, for anything more complex you would need .net remoting.

    If you need an example on how to achieve this using .net remoting please let me know.

     

    Best Regards,

    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 22, 2010 5:23 AM
  • emanual, he is asking about a windows service not a webservice.

     

    parameters are set in the services registry entry, or via the service startup applet using services.mmc


    gimme some slamming techno!!!!
    Friday, October 22, 2010 5:47 AM
  • I way trying to suggest using .net remoting or WCF Services to handle complex requests, i for one prefer the latter.

     

    Best Regards,

    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 22, 2010 5:59 AM
  • Thanks Emanuel, Varga & Blair Allen Stark both.

    Please give me a sample code that I can pass parameters to my methods.

    My parameters is ServerName,DatabaseName,UserID,Password,DirectoryForBackup and etc.

    Also I don't want to use start method because my windows service is running always.

    I just want to use my methods that they catch parameters.

    If is necessary using .net remoting, please bring a sample for that.

    Thanks again.


    Each problem has a solution.
    Friday, October 22, 2010 8:42 AM
  • Hello again,

     

    To achieve this, you will have to host a WCF Service inside the windows service, are you ok with this?

    If yes, then you can host a wcf service inside a windows service using the methods described here

    Afer that you will create a DataContract class, in which you can add all the parameters necessary for the service.

    To call the method from the service you have to create a ServiceContract Interface in which you will have add all the methods you wish to use. A very simple example of creating a wcf client and server can be found here

     

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 22, 2010 9:20 AM
  • read the link i posted
    gimme some slamming techno!!!!
    Friday, October 22, 2010 12:13 PM
  • Hello,

     

    I'm guessing he knows about that, because that is too obvious, but that would mean that every time you need to send some new values you would have to close and restart the service, and that would be ok if the service does not have any other purpose other than listening.

     

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, October 22, 2010 12:26 PM
  • read the link i posted
    gimme some slamming techno!!!!

    I don't want to use start method because it is running every time.

    I want to pass parameters to a my method and that method return a value to my win application .


    Each problem has a solution.
    Friday, October 22, 2010 4:11 PM
  • My question is, why is it a windows service?

    Given the nature of the method you want to call, 'BackupDatabase', wouldn't you want a scheduled task?

    Or, let me get this straight. . . you want a service, running under advanced credentials on the machine, to be available for user of that machine to communicate with so that they can instruct the service to perform a backup given a set of parameters, ie. what server and database.

    Then yes, a WCF service hosted in a windows service is appropriate.

    service runs in background.

    Win app connects to hosted service and exercises one of the methods on the exposed service which returns a value.

    Excuse me for misunderstanding


    gimme some slamming techno!!!!
    Friday, October 22, 2010 4:59 PM
  • I wrote a c# windows service that has a backup methods. I want to install it in sql server machine.(another machine)

    I wrote a windows application in my machine. I want to call backup methods and pass parameters to that. Then it do backup and then return backup file with ".rar" shapes.

    Can I call my methods in windows service and it return a value? (here rar or zip files)

    If I can't call my method, how works the other windows services?

    (for example SMTP,SQL Server,DNS,IIS Admin,Messenger,TCP/IP,...)

    If I can't call my method, Is WCF solution?


    Each problem has a solution.
    Friday, October 22, 2010 5:22 PM
  • Hello,

     

    You cannot do this in a windows service (especially the part with returning a value.

    Other services are also not receiving messages, that's why after some changes to the SQL Server, you have to restart the service.

    Wcf will do all of these and more, and once you get the hang of it you will see that it is actually really easy to create and use.

    The links i've provided earlier should get you started if you decide to use it, if you need any help, please let me know.

     

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by mammadkoma Friday, October 22, 2010 5:55 PM
    Friday, October 22, 2010 5:48 PM
  • A cool example is here:

    http://www.codeproject.com/KB/WCF/WCF_windows_hosting.aspx


    Each problem has a solution.
    • Marked as answer by mammadkoma Saturday, October 23, 2010 6:27 AM
    Saturday, October 23, 2010 6:26 AM
  • Take a look .....

    http://conceptualcsharp.blogspot.com/p/about-service.html

     

    SUnil Jadhav

     


    sunil
    Tuesday, December 27, 2011 1:14 PM