Click here to Skip to main content
6,629,885 members and growing! (21,593 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Communication Foundation » Utilities     Beginner License: The Code Project Open License (CPOL)

WCF DataContract with FaultException Details

By SP.Murugesa Pandian

WCF DataContract
C# (C# 2.0, C# 3.0), Javascript, XML, CSS, HTML, XHTML, .NET (.NET 3.0, .NET 3.5), ASP.NET, SQL Server (SQL 2000, SQL 2005), IIS (IIS 5.1), ADO.NET, WCF, WebForms, Ajax, Design
Version:5 (See All)
Posted:24 Jun 2009
Views:3,834
Bookmarked:9 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.20 Rating: 4.00 out of 5

1

2

3
2 votes, 100.0%
4

5

Introduction

This is a simple WCF service and client application based on Microsoft Enterprise Library 4.1. and FaultException handling. The assumption based on Inventory.Core object for this application is how to use DataContract on the WCF service.

I have two methods, one returns all the products from the Products table as Array output and another one returns Name of the Product when WCF client sends the Product ID as input parameter.

Background

As native ASP.NET programmers, we must consider the SOA aspects when developing the WCF Webservice. Initially I did not care about how my WCF service was going to be functioning when non Microsoft technologies such as PHP, JSP and Perl implement the client application.

It's a good practice while designing the architecture of WCF or Webservice SOA that the developer must give considerable notice to other programming's data types and their type and signatures.

I made two mistakes when I was a newbie to WCF service. I always thought that my WCF service is going to be consumed by only an ASP.NET based application.

Simply we put it this way, "widely recognizable data types" that means general data type which paves way to smooth collaboration between my WCF and unknown client implementation code.

The second was the Exception handling part. I put all WCF related exceptions in my WCF service application such as CommunicationException and TimeoutException, etc. Even when I tested this WCF service on my consumer application, exceptions were not caught thoroughly despite being an ASP.NET client implementation for the WCF.

I don't know what kind of exception will be thrown when JSP or PHP based client application while my WCF service was on the wire.

These two factors were annoying me a lot in my development time. I rectified these problems by making customized error messages as understandable by other technologies.

Using the Code

[FaultContractAttribute(typeof(FaultContractExceptionTest))]
// This will converts the system or application level exception into SOAP header format.
string[] AvailableProducts();   	//Instead of returning the CLR data type 
				//DataSet return the array type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Collections;
ArrayList al = new ArrayList();
string s = "";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand("SELECT * from Products");
DataSet ds = db.ExecuteDataSet(cmd);
foreach (DataRow dRow in ds.Tables[0].Rows)
{
 al.Add(dRow);
 }
foreach (Object row in al)
{
 s = s + ((DataRow)row)["ProductName"].ToString() + ",";

}
string rawString = s.TrimEnd(',').ToString();
string[] result = rawString.Split(',');
return result;

In my available Products implementation, I forcibly convert the DataSet into ArrayList type, then DataRow and array of string. This is because my intention was that JSP and PHP based consumer application should understand what type of data would be received from the WCF Service.

The second notifiable code snippet is as follows:

catch (FaultException<FaultContractExceptionTest> fx)
{
fx.Detail.ErrorMessage = "Internal Server Error " + fx.Reason.ToString();
throw new FaultException<FaultContractExceptionTest>
	(new FaultContractExceptionTest(fx.Detail.ErrorMessage));
}

For FaultException class, use this implementation:

[DataContractAttribute]
public class FaultContractExceptionTest
{
    private string Msg;
     public FaultContractExceptionTest(string msg)
    {
        this.Msg = msg;            
    }
    
    [DataMember]
    public string ErrorMessage
    {
        get { return this.Msg; }
        set { this.Msg = value; }
    }

For SOAP header message level exception handling purpose, I used the FaultContractExceptionTest class as DataContract as you can see here in my WSDL snippet:

<wsdl:operation name="AvailableProducts">
<soap12:operation soapAction=http://tempuri.org/IService/AvailableProducts 
	style="document" /> 
- <wsdl:input>
<wsp:PolicyReference URI="#WSHttpBinding_IService_AvailableProducts_Input_policy" /> 
<soap12:body use="literal" /> 
</wsdl:input>
- <wsdl:output>
<wsp:PolicyReference URI="#WSHttpBinding_IService_AvailableProducts_output_policy" /> 
<soap12:body use="literal" /> 
</wsdl:output>
- <wsdl:fault name="FaultContractExceptionTestFault">
<wsp:PolicyReference 
 URI="#WSHttpBinding_IService_AvailableProducts_FaultContractExceptionTestFault_Fault" /> 
<soap12:fault name="FaultContractExceptionTestFault" use="literal" /> 
</wsdl:fault>
</wsdl:operation> 

History

  • 24th June, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SP.Murugesa Pandian


Member

Location: India India

Other popular Windows Communication Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by SP.Murugesa Pandian
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project