Ok, so you have developed a nice WCF Service with proper implementation of custom fault exception handling, now you want to call this service in a web application or other client application and catch the fault exception in your client code. For simplicity I am using a web page, the following code is self explanatory.
1: Protected Sub btnGetCustomers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetCustomers.Click2:3: Using wcfSrv As CustomerWcfService.CustomerServiceClient = New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")4: Try
5: Dim custArr() As CustomerWcfService.Customer6: custArr = wcfSrv.GetCustomers()7: For Each cust As CustomerWcfService.Customer In custArr8: Response.Write("ID: " & cust.ID.ToString & " Name: " & cust.Name & "<br/>")9: Next
10: lblStatus.Text = "No of Customer Records found: " & custArr.Length.ToString
11:12: 'Custom Fault Exception Response from Service
13: Catch ex As ServiceModel.FaultException(Of CustomerWcfService.ErrorResponse)14: lblStatus.Text = "Code:" & ex.Detail.Code.ToString() & " Messgage:" & ex.Detail.Message & " Details:" & ex.Detail.Details15: wcfSrv.Abort()16:17: 'General Fault Exception
18: Catch ex As ServiceModel.FaultException19: lblStatus.Text = ex.ToString()20: wcfSrv.Abort()21:22: 'General Exception
23: Catch exp As System.Exception24: lblStatus.Text = "Error:" & exp.ToString()
25: wcfSrv.Abort()26:27: Finally
28: wcfSrv.Close()29: End Try30:31: End Using32:33: End Sub34:
Monday, August 17, 2009
Implementing WCF Service Part 6 – Using Fault @ Client
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment