For web application development, usually WCF is hosted in IIS server. However,
No one can guarantee the WCF service would be always running fine in the server. When the server goes wrong then client App should protect itself from blows-up. The below code with “try - catch - using ” statement that it has a mechanism which can check the returning data on WCF service before it is proceeding to next step.
try
{
using
(
ProductsClient.ServiceReference1.ProductsServiceClient proxy = new ProductsClient.ServiceReference1.ProductsServiceClient( )
)
{
//always check the null data returned from server before proceeding to next step//
//codes
}
}
catch (Exception error)
{
//put try-catch to prevent blows-up//
//record the error to DB table//
}
# # #
When we are doing debugging on Visual Studio, the using() statement will hide the exception so we need to comment it out. Otherwise you will get the error below:
"The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state"
try
{
ProductsClient.ServiceReference1.ProductsServiceClient proxy = new ProductsClient.ServiceReference1.ProductsServiceClient( ) ;
// codes go here
}
catch (Exception error)
{
//put try-catch to prevent blows-up//
//record the error to DB table//
}