An unhandled exception in WCF service will cause the communication channel to fault and the session will be lost.
When there is an unhandled exception it faults the server channel.
The client proxy will still be good because the BasicHttpBinding channel is not maintaining session.
Once the communication channel is in fault state, we cannot use the same instance of proxy class, we need to create a new instance of proxy class.
wsHttpBinding channel is maintaining a secure session.
We will use the same Calculator service to understand the concept.please refer Exception Handling in WCF(Part-11) article to create a Calculator service.
We can catch the soap exception for service to display exception instead of white page error. for that, we will make changes in CalculatorClient.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
namespace CalculatorClient { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { int a = Convert.ToInt32(TextBox1.Text); int b = Convert.ToInt32(TextBox2.Text); CalculateService.CalculatorServiceClient client = new CalculateService.CalculatorServiceClient("WSHttpBinding_ICalculatorService"); TextBox3.Text = client.Divide(a, b).ToString(); } catch(FaultException faultException) { TextBox3.Text = faultException.Message; } } } } |
So now when you will run the client and divide by zero then you will see “Attempted to divide by zero” in textbox3.
That is nothing but the SOAP fault message.
Reference books, you may like:
Programming WCF Services: Design and Build Maintainable Service-Oriented SystemsWCF Multi-Layer Services Development with Entity Framework, 4th Edition
Learning WCF: A Hands-on Guide
© 2015, admin. All rights reserved.