dugas
dugas

Reputation: 12453

Create new ChannelFactory<T> when Faulted

What would be the most reliable way of recreating a ChannelFactory<T> in a thread safe manner when it enters a faulted state? This scenario has expected concurrency (let's say 50 concurrent clients for the sake of argument). I would like to know some recommended approaches/thoughts/opinions for achieving this goal (or an alternative).

Edit:

Using @Ladislav Mrnka's answer - it seems the most reliable way to accomplish this is to create a wrapper for ChannelFactory<T>. I ended up doing this, and exposing a CreateChannel method of the wrapper.

Upvotes: 1

Views: 4815

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364299

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.

Upvotes: 4

Kokayca
Kokayca

Reputation: 176

These two creating WCF ChannelFactory<T> and What is the best workaround for the WCF client `using` block issue? wonderful discussions helped me build my own bullet proof WCF service. I believe you too are going to benefit from them greatly. Both include direct answer to your question too :)

Upvotes: 3

Related Questions