开发者

Throwing FaultException<T> from worker thread crashes WCF

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().KillAl开发者_运维问答lHumans(); // Throws a FaultException
                }));


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜