How do you think this solution will help you? You will lock the section so that only one thread can go into that section and check if ChannelFactory
is faulted and recreate it but the instance for channel factory is shared - you return it from the property so:
- If you make a check and create the instance the other thread can receive the factory after that and fault it before you use your new factory in the initial thread (race condition).
- If you recreate a faulted factory all other threads who already hold the reference are still pointing to the faulted one.
So the solution will ensure that ChannelFactory
is recreated in the thread safe manner but you will still have to check if the factory is faulted anywhere you would like to use it (which should again be thread safe to be reliable).
I guess the reliable approach is creating wrapper around ChannelFactory
and handle all complexity with thread safety and checking faulted factory inside the wrapper. The wrapper would expose CreateChannel
method and all other methods you need. You can use such wrapper for managing multiple factories.