简体   繁体   中英

guideline for creating Windows service?

I m working on a Asp.Net MVC application which will have a Windows service.

The Windows service basically call an external Soap based web service which receive some parameters and return data, windows service will store that data into SQL server. This windows service will be calling external web service periodically automatically.

Since I dont have a detail understanding of services. can you please guide me on the following lines:

  1. What can be the technologies to accomplish this ?
  2. Can a Windows service be created using WCF ? If so what protocol will be used for it ?
  3. How to run Windows service while development/ testing ?
  4. How to schedule Windows service calling web service ?

Since your are already using Microsoft technologies, I would suggest building a windows service using C#.

Have a look at that link which has all the details for creating a simple windows service:

Creating a Basic Windows Service in C#

http://www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

You will also need to create a timer where you will call the web service.

Take a look here as well:

What is the best way to implement a "timer"?

Update :

1- What can be the technologies to accomplish this ?

--> C#

2- Can a Windows service be created using WCF ? If so what protocol will be used for it ?

--> No, they are 2 different concepts. But you can host a WCF service in a managed windows service. take a look here: http://msdn.microsoft.com/en-us/library/ms733069.aspx Personally I don't like that approach.

3- How to run Windows service while development/ testing ?

--> You can start/stop/pause a windows service from the Administrative Tools C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Administrative Tools\\services.lnk

4- How to schedule Windows service calling web service ?

--> Start a timer in the windows service "OnStart" method that will call the web service

(1) Look at how to create a class from System.ServiceProcess.ServiceBase and the OnStart() event in particular.

public class MyService : System.ServiceProcess.ServiceBase
{
    protected override void OnStart(string[] args){}
}

(2) A windows service cannot be created from a wcf service call but it may be able to be signaled to start.

(3) I use a Release|Debug switch to allow debugging the service start while in debug mode, however, this is just easy for me.

static void Main()
{
#if (!DEBUG)
    System.ServiceProcess.ServiceBase[] servicesToRun;
    servicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
    System.ServiceProcess.ServiceBase.Run(servicesToRun);
#else
    MyService service= new MyService();
    service.OnStart(null);    
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif 
}

(4) You may need to set up a service agent schedule to run on a timed interval. Look into threading and threadpools and timers for refresh data and service run times.

Visual Studio is good for creating windows services. Not sure which VS version you are using. The Windows Service template and associated functionality is not available in the Standard or Express Editions of Visual Studio. But you can still configure those;

Refer: http://msdn.microsoft.com/en-us/library/vstudio/76477d2t(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Also WCF and Windows services are different; WCF is a replacement for all earlier web service technologies from Microsoft. Windows Services are background services that run without UI.

See: Web Service vs WCF Service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM