Click here to Skip to main content
14,425,416 members
Rate this:
Please Sign up or sign in to vote.
Hi guys,

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!

db7uk 6-Oct-13 17:57pm
I think using marshallByRef in this instance will cause more pain than good. What is wrong with having the Windows service expose endpoints that web, desktop apps can comminicate with? i.e. create a WCF windows service that has a duplex endpoint so that subscribers can be notified of the service results?
staretzu 8-Oct-13 10:33am
Thank you db7uk. I never worked with WCF but I am always open to learning new things. I have read about it and actually started implementing it as it seems much more reliable than marshalling approach. There are a couple of aspects I want your opinion on please:

1. My service will be consumed by PHP client which seems not to like WS that much. In the end it seems that I need to go via BasicHttpBinding, which I've tested and works great.
2. Regarding message Integrity/Authentication. I struggled today to configure the WCF Server with some form of security but failed. You can imagine that working for the first time with WCF I find it hard to get to the end of it. As I am only interested in integrity and authenticity (I don't really care if someone sees the message transmitted as long as they cannot interfere), I decided to build my own protocol as follows:
PHP Side
- define a password known on WCF and PHP side
- concatenate the message with the password, and apply a hash algorithm (SHA1)
- concatenate the original message with the hash and send it via SOAP

WCF side:
- get the message via SOAP
- split between original message and hash
- concatenate original message with known password and apply hash
- compare hashes

What do you think?

db7uk 23-Oct-13 2:56am

Sorry for the late reply. Yes, wcf can be a mine field with it comes to security. Your suggested route does seem feasible and is actually something I implemented a while back but with normal SOAP web services. WCF does have many security features governing transport, message or transport and message encryption. All with their own headaches. Anyway, Go for your suggestion if you feel the risks are not high.

Good luck with the project.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100