none
WCF - Windows Service RRS feed

  • Question

  • Hello Experts,

    Am new to C# to develop a windows service do I need to learn WCF or something else. Kindly let me know what should be my area of focus to develop a windows service that runs in background  to execute queries on SQL Server.

    Thanks 

    Priya

    Friday, October 4, 2019 6:06 PM

Answers

  • Given that you want to run SQL queries every 10 seconds or so then there would be no benefit of using WCF. WCF, REST, gRPC are RPC components. These are IPC components. If you don't need interprocess communication (e.g. you calling external processes for stuff or somebody calling you for stuff) then none of these technologies are needed. Just write a windows service. There are plenty of examples in MSDN and in blog articles on how to do this.

    However be warned that writing/debugging services isn't trivial. You are strongly recommended to write your service as a console app that can run either standalone or as a service. For debugging you'll run it standalone. There are plenty of blog articles on how to do this. Additionally you might look into using Topshelf which as a simple framework on top of it to make it easier.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Priya Bange Thursday, October 10, 2019 4:18 AM
    Wednesday, October 9, 2019 1:35 PM
    Moderator

All replies

  • Hello Experts,

    Am new to C# to develop a windows service do I need to learn WCF or something else. Kindly let me know what should be my area of focus to develop a windows service that runs in background  to execute queries on SQL Server.

    Thanks 

    Priya

    A Windows service is a Windows desktop solution that is continually running as a service hosted by the Windows O/S. A WCF service uses various protocols that allow communications between the WCF clinet program with the WFC service program.

    A Windows service program can host a WCF client software acting as a client to a WCF service, or the Windows service can host a WCF service software acting as a service to a WCF client.

    https://stackify.com/what-are-windows-services/

    https://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF-2

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-host-a-wcf-service-in-a-managed-windows-service

    You don't need WCF, and I don't know if you need a Windows service program either. Maybe, all you need is a Windows Console program.

    Friday, October 4, 2019 11:52 PM
  • Who will request the queries to run? What type of queries are they? Why can't the app that needs to talk to the DB query directly? Is there some sort of scheduling for these queries? Is there a single or multiple apps that will call this service?

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, October 7, 2019 2:02 PM
    Moderator
  • Hello,

    Windows service is windows service - program running on server. There are some limitation on what can and what can't be done on such type of program.

    WCF is a technology to communicate between two system over HTTP protocol.
    Could it be used to communicate with SQL SERVER? - No, it could not - it communicate between two system.
    Could you get data from SQL SERVER over WCF - yes, you can. But WCF communication will be between two system and on one (or both) side of WCF-software could communicate with SQL SERVER.

    The rest depends on what exactly you have in your requirements.


    Sincerely, Highly skilled coding monkey.

    Monday, October 7, 2019 2:31 PM
  • Hi Priya Bange,

    Since your question is more related to WCF, I will move this case to WCF forum.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 8, 2019 1:45 AM
  • Who will request the queries to run? What type of queries are they? Why can't the app that needs to talk to the DB query directly? Is there some sort of scheduling for these queries? Is there a single or multiple apps that will call this service?

    Michael Taylor http://www.michaeltaylorp3.net

    Yes, there will be scheduling to execute the queries. Some need to be executed after every 5 second , some may even need to be executed after every 1-2 sec.

    Thanks

    Priya

    Tuesday, October 8, 2019 3:46 AM
  • But by who? Is there some external process that runs that needs to run these queries? Why can't they run the queries directly. Basically the answers indicate what you need.

    Windows service - something needs to be running all the time either doing work or listening for events. Needs to run whether someone is logged in our not.

    Scheduled task/console app - needs to run periodically, whether someone is logged in or not. Does its work and shuts down until the next time it is needed.

    REST API (preferable), WCF service or gRPC - needs to be called by other processes on demand to do work.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, October 8, 2019 5:07 AM
    Moderator
  • Hi,

    WCF can perform CURD operation on database. The specific operation is determined by the client invocation. Under your circumstance, Windows NT service might be a container, which host and run the WCF service properly. since the Windows NT service normally has two events, OnStart and OnStop. These events are performed when service is started and closed. If we want to implement some complicated business logic, it is advisable to host the WCF service in Windows NT service, and perform the CRUD operation in WCF. If we want to perform an operation periodically, we could also set a timer in the OnStart event. But I don’t recommend this.
    Feel free to let me know if there is anything I can help with
    Best Regards
    ABraham

    Tuesday, October 8, 2019 10:04 AM
    Moderator
  • Thank you everyone for the detailed explanation. 

    Am trying to replicate the design advised by Sir in the below thread. Please let me know what should be my area of focus to develop such a solution.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/9ea803a9-320d-4744-9e68-e41e2bae772a/windows-service-scheduler?forum=csharpgeneral#bc711af4-7a84-498f-a116-4e1ba8a8785f

    Thanks again

    Priya


    • Edited by Priya Bange Wednesday, October 9, 2019 4:02 AM .
    Wednesday, October 9, 2019 3:38 AM
  • Given that you want to run SQL queries every 10 seconds or so then there would be no benefit of using WCF. WCF, REST, gRPC are RPC components. These are IPC components. If you don't need interprocess communication (e.g. you calling external processes for stuff or somebody calling you for stuff) then none of these technologies are needed. Just write a windows service. There are plenty of examples in MSDN and in blog articles on how to do this.

    However be warned that writing/debugging services isn't trivial. You are strongly recommended to write your service as a console app that can run either standalone or as a service. For debugging you'll run it standalone. There are plenty of blog articles on how to do this. Additionally you might look into using Topshelf which as a simple framework on top of it to make it easier.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Priya Bange Thursday, October 10, 2019 4:18 AM
    Wednesday, October 9, 2019 1:35 PM
    Moderator