Top Banner
Q8M1 – SC Dudy Fathan Ali S.Kom Distributed Application Development Exception Handling in WCF Services Dudy Fathan Ali, S.Kom (DFA) 2017 CEP - CCIT Fakultas Teknik Universitas Indonesia
17

Exception Handling in WCF Services

Jan 22, 2018

Download

Technology

Dudy Ali
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Distributed Application Development

Exception Handling in WCF Services

Dudy Fathan Ali, S.Kom (DFA)2017

CEP - CCITFakultas Teknik Universitas Indonesia

Page 2: Exception Handling in WCF Services

Additional Documents

Q8M1 – SC Dudy Fathan Ali S.Kom

bit.ly/4sc1-repo

Download this Powerpoint Slide:

Page 3: Exception Handling in WCF Services

Objectives

Q8M1 – SC Dudy Fathan Ali S.Kom

Define Exception Handling

Implement Exception Handling

Page 4: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Define Exception Handling

Distributed Application Development

o Anticipating errors and providing error-handling code is known as exception handling.

o A WCF service and a client application communicate with each other by using SOAP.

o When an error occurs in the service, it needs to be communicated to the client in order to be handled properly.

Page 5: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Exception Communication by Using SOAP

Distributed Application Development

o When an unhandled exception occurs in a WCF service, a default exception is thrown and the communication between the service and the client application is discontinued.

o The exception thrown by a WCF service should be converted into the SOAP format so that any client can easily interpret it.

o The SOAP specification is used to convert the exception into a standard format.

o The SOAP specification provides a fault element that may be present in the body of the message sent by a WCF service to a client application.

Page 6: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Exception Communication by Using SOAP

Distributed Application Development

o The SOAP fault element is a standard format that contains the error message and information related to the error message.

o When WCF receives an unhandled exception at the service host, it faults the server channel and further communication between the clients and the service is halted.

Page 7: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Implementing Exception Handling

Distributed Application Development

o Exception handling is implemented to handle all the possible exceptions.

o To implement exception handling in a WCF service, you can use the following classes of the System.ServiceModel namespace:

o The FaultException class

o The generic FaultException class

Page 8: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the FaultException Class

Distributed Application Development

o The FaultException class is a base class that is used to handle exceptions in WCF.

o This class serializes the error messages that are thrown by a WCF service into the SOAP fault.

o The following code snippet shows the use of the FaultExceptionclass:

public int divideNumber(int num1, int num2)

{

if (num2 == 0)

{

throw new FaultException("Pembagi tidak boleh bernilai 0.");

}

int result = num1 / num2;

return result;

}

Page 9: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the FaultException Class

Distributed Application Development

o The following code snippet shows how a client application can access the exception class:

public void divideClient()

{

ServiceReference.ServiceClient obj;

obj = new ServiceReference.ServiceClient();

try

{

int result = obj.divideNumber(10, 0);

Console.WriteLine(result);

}

catch (FaultException faultEx)

{

Console.WriteLine(faultEx.Message);

}

}

Page 10: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o To enable a client application to access a user-defined class, you need to expose the class.

o To expose a user-defined exception class and send a customized message to a client application, you need to perform the following tasks:

o Define a user-defined exception class

o Define a FaultContract

o Throw a FaultException

Page 11: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o Define a user-defined exception class:

o While defining a user-defined exception class, you need to use the [DataContract] and [DataMember] attributes to expose the class and its members, as shown in the following code snippet:

[DataContract]

public class MyFaultException

{

[DataMember]

public string message;

}

Page 12: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o Define a [FaultContract]:

o The [FaultContract] attribute is defined to serialize the structure of the user-defined exception class.

o It indicates to a WCF service that a service’s WSDL should include the customized exception message sent to a client application.

Page 13: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o The following code snippet shows how to use the FaultContractattribute:

[ServiceContract]

public interface IService

{

[OperationContract]

[FaultContract(typeof(MyFaultException))]

int Divide(int numerator, int denominator);

}

[DataContract]

public class MyFaultException

{

[DataMember]

public string message;

}

Page 14: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o Throw a FaultException:

o To send a customized message in a SOAP fault format, you need to throw a FaultException exception that takes a user-defined class as a parameter.

Page 15: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o The following code snippet shows how to use the Generic FaultException class:

public int divideNumber(int num1, int num2)

{

if (num1 < num2)

{

MyFaultException objError = new MyFaultException();

objError.message = "Value of denominator can not be

greater than numerator";

throw new FaultException<MyFaultException>(objError);

}

int result = num1 / num2;

return result;

}

Page 16: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Using the Generic FaultException Class

Distributed Application Development

o The following code snippet shows how a client application can access the user-defined exception class:

public void divide()

{

ServiceRef.ServiceClient obj;

obj = new ServiceRef.ServiceClient();

try

{

int result = obj.divideNumber(10, 11);

Console.WriteLine(result);

}

catch (FaultException<ServiceRef.MyFaultException> faultEx)

{

Console.WriteLine(faultEx.Detail.message);

}

}

Page 17: Exception Handling in WCF Services

Q8M1 – SC Dudy Fathan Ali S.Kom

Thank You!Dudy Fathan Ali, S.Kom

[email protected]