views:

124

answers:

2

What I found out was if you throw a FaultException from a new worker thread, it doesnt percolate up to the client but just crashes WCF.

Any solutions???

example:

var thread = new Thread(new ThreadStart(
                delegate
                {
                    new Killbot().KillAllHumans(); // Throws a FaultException
                }));
A: 

The simplest way would be to wrap the call in a try-catch block and log the exception:

var thread = new Thread(new ThreadStart(
            delegate
            {
                try
                {
                    new Killbot().KillAllHumans(); // Throws a FaultException
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.ToString());
                }
            }));

If you want to handle the exception in your main thread you would have to use BeginInvoke and EndInvoke in combination with an AsyncCallback.

0xA3
A: 

Personally I would not bother with background threads in a WCF service. A service is effectively a "background worker" anyway. All you need to do is ensure that any blocking calls you make inside the service don't affect other clients. You can do this by changing the concurrency mode:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
class MyServiceClass : IMyServiceContract {
    public void KillAll() {
        new Killbot().KillAllHumans(); // Throws a FaultException
    }
}

When that is set, WCF will call your service methods on multiple threads with no attempt to synchronise them. As long as you write your code with this in mind, you can do all the blocking calls you want.

Christian Hayter