Introduction:
In this article,i am going to explain about how to handling exceptions in wcf services using fault exception.
Main:
Fault Handling in WCF:
The default behavior for WCF is not to return that exception to the client unless it is thrown as a FaultException
(or inherited subtype) or if a FaultContract is implemented.
See this below simple examble,
Service Code:
using System.Data;
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Threading;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
namespace Products
{
// Simplified service contract
[ServiceContract(Namespace = "http://NetProgrammingHelp.com/2006/09/30", Name = "SimpleProductsService")]
public interface ISimpleProductsService
{
[OperationContract(Name = "ListProducts")]
List<string> ListProducts();
}
// WCF service class that implements the service contract
public class ProductsServiceImpl : ISimpleProductsService
{
public List<string> ListProducts()
{
// Read the configuration information for connecting to the NetPrgHlp database
Database dbNetPrgHlp;
try
{
dbNetPrgHlp = DatabaseFactory.CreateDatabase("NetPrgHlpConnection");
}
catch (Exception e)
{
throw new FaultException("Exception reading configuration information for the NetPrgHlp database: " + e.Message, new FaultCode("CreateDatabase"));
}
// Retrieve the details of all products by using a DataReader
IDataReader productsReader;
try
{
string queryString = @"SELECT ProductNumber
FROM Production.Product";
productsReader = dbNetPrgHlp.ExecuteReader(CommandType.Text, queryString);
}
catch (Exception e)
{
throw new FaultException("Exception querying the NetPrgHlp database: " + e.Message, new FaultCode("ExecuteReader"));
}
// Create and populate a list of products
List<string> productsList = new List<string>();
try
{
while (productsReader.Read())
{
string productNumber = productsReader.GetString(0);
productsList.Add(productNumber);
}
}
catch (Exception e)
{
throw new FaultException("Exception reading product numbers: " + e.Message, new FaultCode("Read/GetString"));
}
//Return the list of products
return productsList;
}
}
}
using System.Data; using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using System.Runtime.Serialization; using System.Threading; using System.Security; using System.Security.Permissions; using System.Security.Principal; namespace Products { // Simplified service contract [ServiceContract(Namespace = "http://NetProgrammingHelp.com/2006/09/30", Name = "SimpleProductsService")] public interface ISimpleProductsService { [OperationContract(Name = "ListProducts")] List<string> ListProducts(); } // WCF service class that implements the service contract public class ProductsServiceImpl : ISimpleProductsService { public List<string> ListProducts() { // Read the configuration information for connecting to the NetPrgHlp database Database dbNetPrgHlp; try { dbNetPrgHlp = DatabaseFactory.CreateDatabase("NetPrgHlpConnection"); } catch (Exception e) { throw new FaultException("Exception reading configuration information for the NetPrgHlp database: " + e.Message, new FaultCode("CreateDatabase")); } // Retrieve the details of all products by using a DataReader IDataReader productsReader; try { string queryString = @"SELECT ProductNumber FROM Production.Product"; productsReader = dbNetPrgHlp.ExecuteReader(CommandType.Text, queryString); } catch (Exception e) { throw new FaultException("Exception querying the NetPrgHlp database: " + e.Message, new FaultCode("ExecuteReader")); } // Create and populate a list of products List<string> productsList = new List<string>(); try { while (productsReader.Read()) { string productNumber = productsReader.GetString(0); productsList.Add(productNumber); } } catch (Exception e) { throw new FaultException("Exception reading product numbers: " + e.Message, new FaultCode("Read/GetString")); } //Return the list of products return productsList; } } } |
Client Code:
using System.ServiceModel;
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Channels;
namespace ProductsClient
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press ENTER when the service has started");
Console.ReadLine();
try
{
// Create a default HTTP binding
BasicHttpBinding httpBinding = new BasicHttpBinding();
// Create an endpoint
EndpointAddress address = new EndpointAddress(
"http://localhost:8040/SimpleProductsService/SimpleProductsService.svc");
IChannelFactory<IRequestChannel> factory =
httpBinding.BuildChannelFactory<IRequestChannel>();
factory.Open();
IRequestChannel channel = factory.CreateChannel(address);
channel.Open();
Message request = Message.CreateMessage(MessageVersion.Soap11,
"http://NetPrgHlp.com/2006/09/30/SimpleProductsService/ListProducts");
Message reply = channel.Request(request);
Console.Out.WriteLine(reply);
request.Close();
reply.Close();
channel.Close();
factory.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.Message);
}
Console.WriteLine("Press ENTER to finish");
Console.ReadLine();
}
}
}
using System.ServiceModel; using System; using System.Collections.Generic; using System.Text; using System.ServiceModel.Channels; namespace ProductsClient { class Program { static void Main(string[] args) { Console.WriteLine("Press ENTER when the service has started"); Console.ReadLine(); try { // Create a default HTTP binding BasicHttpBinding httpBinding = new BasicHttpBinding(); // Create an endpoint EndpointAddress address = new EndpointAddress( "http://localhost:8040/SimpleProductsService/SimpleProductsService.svc"); IChannelFactory<IRequestChannel> factory = httpBinding.BuildChannelFactory<IRequestChannel>(); factory.Open(); IRequestChannel channel = factory.CreateChannel(address); channel.Open(); Message request = Message.CreateMessage(MessageVersion.Soap11, "http://NetPrgHlp.com/2006/09/30/SimpleProductsService/ListProducts"); Message reply = channel.Request(request); Console.Out.WriteLine(reply); request.Close(); reply.Close(); channel.Close(); factory.Close(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.Message); } Console.WriteLine("Press ENTER to finish"); Console.ReadLine(); } } } |
Conclusion:
Hope this helps,
Happy coding.
I truly loved this brilliant article. Please continue this awesome work. Regards, Duyq.
Usually I do not post on blogs, but I would like to say that this article really forced me to do so! Thanks, really nice article.
I knew I had been correct. My friend and I placed a bet about which web site was superior. I thought your webpage was much better created, but she believed this post on trendy style ideas was much better. We rounded up 5 loved ones memebers who experienced not observed possibly web site earlier to to study them every more than. Majority chose your site. Thanks for maintaing a great website.
Seriously you should retake 4th grade… your grammar is atrocious! My eyes hurt, thanks.
You are a very smart person!
Great man, glad to see this post, and I find your blog is so comfortable for me, just bookmark it to my social media…Thanks for sharing.All of my friends will see this.
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later
Thanks for Providing such a valuable Information!