I’m creating an application with a NetTcpBinding service and a BasicHttpBinding one.
Through these services, the clients send requests to the application: these requests must be put into a Queue object, that is the queue of inbound request.
- Both services must allow concurrent calls from the clients.
- This means that both services should access concurrently to the queue in order to add the received requests.
- In addition, another thread must access the queue in order to get and process the requests.
I could use ConcurrencyMode.Multiple and so many calls could be taken simultaneously. However, this does not guarantee a concurrent access to the queue. Should I put the two ServiceHost in two different thread? For example:
- The first thread dequeues and processes the requests in the queue.
- The second thread instantiates the
NetTcpBindingservice and enqueues new requests in the queue. Moreover, it sends any replies via callback. - The third thread instantiates the
BasicHttpBindingservice and enqueues new requests in the queue.
This is my idea.
Since I am almost a newbie, I would be grateful if you could give me some advice.
Maybe I should start writing three threads that access the queue concurrently: for the moment the first two threads may enqueue random requests to the queue, while the third thread consume these requests.
Since you characterize yourself as “I am almost a newbie” I would suggest you more general reading – Threading in C# Joseph Albahari free online article/book. It thoroughly reviews problems and instrumentation of threading in C#.
I assure you, careful reading of parts 3 and 4 (and reviewing part 1 and 2) will give you the tools to solve not only your current problem but a bunch of many other problems that you will meet on the way of multithreading.
If you just want a quick answer to your question, just follow Yahia’s answer.