-1

I have to do a windows service that writes the date and time on the database each time a card reader is scanned.

Can you please give me some instructions where to start or how to start.

Any book, website , example code will be appreciated.

Is my first time to work with windows service.

Thorsten Dittmar
  • 52,871
  • 8
  • 78
  • 129
Beslinda N.
  • 3,683
  • 5
  • 20
  • 32
  • Welcome to Stack Overflow. This is not a good way to ask a question here. Did you try anything so far to solve your problem? Show your effort first so people might show theirs. Please read [FAQ](http://stackoverflow.com/tour), [How to Ask](http://stackoverflow.com/help/how-to-ask) and [help center](http://stackoverflow.com/help) as a start. – Nahuel Ianni Aug 20 '14 at 07:23
  • I am just looking for instruction from someone who understand windows services. I am not looking for a ready code. – Beslinda N. Aug 20 '14 at 07:25
  • You need a way to know when the card reader is used, and you need to know how to create Windows services with .NET. This has nothing to do with ASP.NET MVC. How to create Windows services can easily be found in MSDN etc, just Google it... – Roy Dictus Aug 20 '14 at 07:31
  • I am using a card reader to get the date and time and then I want the service to write those information in the database. I have already done a mvc4 app to show the data. – Beslinda N. Aug 20 '14 at 07:40

1 Answers1

3

Tipps:

  1. Don't start out implementing a service. Implement a console application and put the real functionality into a DLL (you will probably need to monitor a serial port or similar, so you need to be multi-threaded anyway). Later, use that DLL in your service. Services are more difficult to debug than applications.
  2. Start Visual Studio and create a new project from the Windows Service template. Add a reference to your DLL. You will see an OnStart and OnStop event. First step: In OnStart start the thread that monitors the device, in OnStop, stop that thread.
  3. Provide proper names and descriptions to the service using the Properties window.
  4. Provide a project installer (that's not a setup program, but a class that can install/uninstall the service, which will be called by the setup program). Inspect and adjust all properties.
Thorsten Dittmar
  • 52,871
  • 8
  • 78
  • 129
  • I use extra method `StartToDebug()` for debugging services - it is conditionally compiled using `#if Debug` statement. See http://stackoverflow.com/questions/125964/easier-way-to-start-debugging-a-windows-service-in-c-sharp – Vojtěch Dohnal Aug 20 '14 at 07:46
  • Actually, nowadays I don't use the DLL approach anymore. What I actually do is change the `Main` function so that based on `Environment.UserInteractive` I either start the service or I create a normal Windows Forms application that manually creates an instance of the service class and calls new `DoStart` and `DoStop` methods accordingly. That way I can start as a Windows Forms application and as a service, which makes debugging really easy. But I thought that for a beginner, that was a pretty advanced approach, so I suggested the use of a DLL. – Thorsten Dittmar Aug 20 '14 at 07:51
  • @Thorston Dittmar I think this is what I need. I will try to figure it out according to what you suggested. Thank you. – Beslinda N. Aug 20 '14 at 07:54
  • @BesaNeziri In addition to what I wrote above I suggest you first of all create a simple service that just logs a timestamp into the database to get a feeling for how things work. – Thorsten Dittmar Aug 20 '14 at 08:13
  • I did that. But I am bit confused . I am working on RFID card reader. I need to get the data from the port of the RFID and write them in my database. – Beslinda N. Aug 20 '14 at 08:17
  • So are you confused about how to create a service or how to handle the card reader? Please decide and maybe ask a new question :-) – Thorsten Dittmar Aug 20 '14 at 08:18
  • OK I know how to open the card reader but I am confused of the fact now. How to get the time and date the card reader was scanned. And how the windows service will work with the card. Yeah maybe you are right. I need to make a new question :/ – Beslinda N. Aug 20 '14 at 08:21
  • Well, in that case I suggest you first write a demo application that just interacts with the card reader - that has nothing to do with how a Windows Service works. – Thorsten Dittmar Aug 20 '14 at 08:40