1

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.

dugas
  • 12,025
  • 3
  • 45
  • 51
  • 1
    Mhh, a channel factory which can fault and has a communication state seems weird to me. I would consider communication state more a channel property. A factory which can fall over does not seem like a robust design. – ChrisWue Jun 03 '11 at 07:52
  • 1
    @ChrisWue - I did not implement the ChannelFactory - it is a part of the .Net framework and has a State property that it inherits from the CommunicationObject in the .Net framework. And thanks for the feedback - I agree that having the ChannelFactory being able to fault is an undesirable attribute, which is why I would like to be able to handle it to make using it more robust. – dugas Jun 04 '11 at 01:13

2 Answers2

4

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.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks @Ladislav for the valuable feedback. I agree with your points, they make sense, and using a wrapper and returning the created Channel proxy from a wrapper instead of allowing other threads to hold a reference to the ChannelFactory is the way to go. To make sure I understand correctly - your main point is to implement the wrapper to ensure callers get a Channel proxy that was created from a non-faulted ChannelFactory (the wrapper is responsible for implementing the logic that deals with issues when creating a Channel, not the caller that accesses the wrapper)? – dugas Jun 04 '11 at 01:25
3

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 :)

Community
  • 1
  • 1
Chiao
  • 176
  • 2
  • 6