I'm preparing to work on a project and need some 'best practice' advise. In this project I have to read/write from/to a USB Device at predefined intervals, and I have to share results on both web and desktop applications. Besides this I need to save logs to database.
I need to create this in C# and I was thinking on building a Windows Service that will periodically communicate to USB device using a Windows Timer and save current state ob USB device in a custom object that I'll create. I can then use another Windows Timer to write to DB from the object. Then I will share this object using MarshallByRef, by creating a couple of public methods that can manipulate data and I will create both HTTP and TCP Channels on different ports so that the information can be called either from the internal network or via web using SOAP.
I never worked with shared objects and I am still trying to discover the best way of doing this. A couple of questions are:
- The object that holds the current state of USB device is a static object that needs to be written internally from the windows service and should be made available to externals that can, in turn, amend it (the object). Everyone needs to see the same instance of the object. Do I need to put some lock on the object when I'm writing to it? Or maybe when externals are reading it? Just thinking that the same object might be called by externals at the same time when the Windows Service is writing it and this might cause problems.
- Do I need to make the class (shared by Marshall) a SingleTon class? Or is it enough that I'm configuring the channels as Singleton?
- Is it OK to have 2 channels open at the same time for the same object?
Any other advise would be much appreciated!