I am new to WCF and trying to build a demo WCF project with WebHttpBinding
binding.
The projects work fine but I am stuck with concept of fault exception. Below is the code I am using to generate fault exception but on client side what I get is (400) Bad Request error and the InnerException is null. Where am I getting wrong?
public DataTable GetCreativesData(string id,string type)
{
try {
DataAccess da = new DataAccess();
string qryStr = @"select * from dbo.tbMyTable";
if (type != null && type != "")
qryStr += " WHERE1 type='"+type+"'";
DataTable dt = da.GetDataTable(qryStr);
return dt;
}
catch (FaultException ex) {
throw new FaultException("Improper Query");
}
}
A 400 error is correct for a fault exception, since you are indicating to the user that something is wrong likely with what they provided.
There is no inner exception when you raise the FaultException
, since a core concept of FaultException
is that it works across technologies (interoperability) & the concept of an inner exception is very specific to .NET.
If you really wanted to pass an Exception
down, you could use the FaultException which allows you to specify a generic parameter and then you set that to Exception
. You would then create and raise that.
Another aspect to remember is how you are handling your errors, in your code you are throwing a brand new exception. That means that all the information in the variable ex, is lost. Unless you intend to hide something, you could try this
catch (FaultException ex)
{
throw;
}
This option takes the original FaultException
and raises it again, thus preserving all the information in it.
Lastly and outside your question, that SQL scares me - looks like it could be vulnerable to a SQL injection attack - just something you may want to check.