Using Custom Fault Exception in WCF

This entry was posted by admin Friday, 11 February, 2011
Read the rest of this entry »

using Custom Fault Exception

  1. Create a new WCF Service Library and name it as ‘ProductServiceLibrary’.
  2. Rename the default service interface and service implementation files as ‘IProductsService.cs’ and ‘ProductsService.cs’.
  3. Open ‘IProductsService.cs’ and modify the code to look like the one below.

using System.Collections.Generic;

using System.Runtime.Serialization;

using System.ServiceModel;

namespace ProductsServiceLibrary

{

[ServiceContract]

public interface IProductsService

{

[OperationContract]

[FaultContract(typeof(ConnectionFault))]

[FaultContract(typeof(DataReadFault))]

List<Product> GetProducts();

}

[DataContract]

public class Product

{

[DataMember]

public int ProductId;

[DataMember]

public string ProductName;

[DataMember]

public string Category;

}

[DataContract]

public class ConnectionFault

{

[DataMember]

public string operation;

[DataMember]

public string message;

[DataMember]

public string Reason;

}

[DataContract]

public class DataReadFault

{

[DataMember]

public string operation;

[DataMember]

public string message;

[DataMember]

public string Reason;

}

}

  1. Open ‘ProductsService.cs’ and modify code to look like the following.

using System;

using System.Collections.Generic;

using System.ServiceModel;

using System.Data.SqlClient;

namespace ProductsServiceLibrary

{

public class ProductsService : IProductsService

{

#region IProductsService Members

public List<Product> GetProducts()

{

List<Product> products = new List<Product>();

using (SqlConnection conn = new SqlConnection(“server=(local); integrated security=true; initial catalog=Northwind”))

{

try

{

conn.Open();

}

catch (Exception ex)

{

ConnectionFault cf = new ConnectionFault();

cf.operation = “ProductsServiceLibrary.ProductsService.GetProducts”;

cf.message = ex.Message;

throw new FaultException<ConnectionFault>(cf);

}

using (SqlCommand cmd = new SqlCommand(@”Select p.ProductId, p.ProductName, c.CategoryName

from Products p

inner join Categories c on c.CategoryId = p.CategoryId

Order by p.ProductName”, conn))

{

try

{

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

{

Product prod = new Product();

prod.ProductId = reader.GetInt32(0);

prod.ProductName = reader.GetString(11);

prod.Category = reader.GetString(2);

products.Add(prod);

}

}

catch (Exception ex)

{

DataReadFault drf = new DataReadFault();

drf.operation = “ProductsServiceLibrary.ProductsService.GetProducts”;

drf.message = ex.Message;

drf.Reason = “Data read failure”;

throw new FaultException<DataReadFault>(drf);

}

}

}

return products;

}

#endregion

}

}

  1. Here is the code for ‘GetData’ button’s click event.

private void GetDataButton_Click(object sender, EventArgs e)

{

try

{

Product[] products = proxy.GetProducts();

productBindingSource.DataSource = products;

}

catch (FaultException<ConnectionFault> cf)

{

MessageBox.Show(“Error\n” + cf.Detail.message + “\noccured in ” + cf.Detail.operation, “Connection error”);

}

catch (FaultException<DataReadFault> drf)

{

MessageBox.Show(“Error\n” + drf.Detail.message + “\noccured in ” + drf.Detail.operation, “Data read error”);

}

catch (FaultException fe)

{

MessageBox.Show(“Unrecognized error”);

}

}

By

Team CVK

Microsoft Competency

www.cvktech.com


Leave a Reply