ExceptionShielding with AsyncPattern=true in wcf

Topics: Exception Handling Application Block
Feb 19, 2013 at 6:34 AM
Hi guys ,
is it possible to use ExceptionShielding with asyncpattern =true .becuse we not able to get the fault exception at client when there is some exception in service code and says that exception was unhandled but its working fine when we did not use asyncpattern =true.
Feb 20, 2013 at 7:47 AM
I have a sample working so it is possible. You can view it here.

If that doesn't help, then please post some more details in order to recreate your specific issue.

Randy Levy
Enterprise Library support engineer
Support How-to
Feb 20, 2013 at 7:51 PM
Thanks Randey ,
Yes it working fine with your project but all things are same in my projects as well but we using asyncpattern in different manner like sending the request and getting the response objects and its not working with my project .
i have create 2 project one for the wcf service and other unit test so we can test this functionality by using visual studio test project .
I am shared these 2 projects through skydive with zip file named as Exceptionwebservice .Let me know if you r not able to run or not find the shared file
In service contract has 2 method .

With Asyncpattren---Its not working as Expected
1) [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginCreateOrder(Addrequest addrequest, AsyncCallback callback, object asyncState);
    AddResponse EndCreateOrder(IAsyncResult result);
For without Asyncpattern

2)CreateOrder1--Its working Fine as expected
    [OperationContract(Name = "CreateOrder1")]
    int CreateOrder1(string currency, double amount);
I have create the unit test project in which we are consuming this service .In this test method we have 2 method
  1. Testvalidation--This will call the Asyncpattren pattren function(CreateOrder)and its not working fine
  2. Testvalidation---This will call the CreateOrder1 fuction which is not using Asyncpattren and its working fine .
Kindly Share the suggestions and let me as well if there is any problem in my code or i am doing something wrong

Feb 20, 2013 at 7:58 PM
link for open this project
and let me know if i am doing some thing wrong
Feb 21, 2013 at 6:27 AM
It looked like you were referencing Unity version 1.2 instead of 2.1. Maybe just a versioning issue with the project?

Anyway, the main difference between the working and non-working examples is that in the non-working example the actual WCF service is spawing a Thread Pool thread (QueueUserWorkItem()) to do the work whereas in the working example the service does the work (and throws the exception) on the main request thread.

It looks to me that when starting a new thread the OperationContext is lost (since it's associated with the main request thread) and it appears that the ErrorHandler is associated with the OperationContext (via OperationContext.Current.EndpointDispatcher.ChannelDispatcher.ErrorHandlers). So, the ExceptionShielding IErrorHandler never runs from the thread pool thread.

The approach to take would be to run your application logic on the main worker thread.

Randy Levy
Enterprise Library support engineer
Support How-to