Sharing objects between threads in C # and WCF

I have a server that provides a WCF SOAP service endpoint. This server also uses a group communication infrastructure called Ensemble (not related to the issue) to communicate with other servers in the same cluster.

I need to exchange objects / data between a separate thread that listens for incoming messages from other servers and threads that run WCF routines when they are called. So far, I have been doing the simplest thing that I could think of โ€” I created a static โ€œdatabaseโ€ class with static elements and static methods โ€” and used synchron () to synchronize. That way, I could access this class both from the server and from the group communication stream. My problem is that it kind of breaks down the whole โ€œOOP thingโ€, and I think something more clever can be done here ...

+4
source share
2 answers

If the only problem you are facing with your solution is its "non-OOP-edness", you can go for the Singleton Pattern instead. This is a widely used pattern for situations where you must have one instance of a class that must be shared between several parts of the system that are otherwise disabled. The template remains somewhat controversial, because some consider it an illustrious version of the global variable, but it is effective in the task.

+3
source

Encapsulate a separate thread that listens for incoming messages from other servers in the class, says MyCustomService.

Write a WCF service. Implementation class with behavior like concurrencyMode multiple and InstanceContextMode Single

Record the delagate event inside the WCF service implementation class. The delegate will return the class type MyCustomService.

When you programmatically create a WCF service (host.Open), first set the delegate to a function that will return an instance of MyCustomService, which can be single-point or static.

In a service instance class, you can always call a delegate to get an instance of MyCustomService. Check the zero value.

0
source

Source: https://habr.com/ru/post/1391101/


All Articles