I’m having a mind blowing problem using WCF 4.0 RESTful service. I am trying to make a rest service that will return, in case of an error, a xml document describing the problem
ex :
<ErrorHandler>
<cause>Resource not available</cause>
<errorCode>111103</errorCode>
</ErrorHandler>
In order to make this i’ve created a default REST service using the template provided by visual studio
Here is my service Class :
public class Service1
{
// TODO: Implement the collection resource that will contain the SampleItem instances
[WebGet(UriTemplate = "")]
public List<SampleItem> GetCollection()
{
// TODO: Replace the current implementation to return a collection of SampleItem instances\
// throw new WebException("lala");
throw new WebFaultException<ErrorHandler>(new ErrorHandler { cause = "Resource not available", errorCode = 100 }, System.Net.HttpStatusCode.NotFound);
//return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
}
[WebInvoke(UriTemplate = "", Method = "POST")]
public SampleItem Create(SampleItem instance)
{
// TODO: Add the new instance of SampleItem to the collection
return new SampleItem() { Id = 3, StringValue = "59" };
}
[WebGet(UriTemplate = "{id}")]
public SampleItem Get(string id)
{
// TODO: Return the instance of SampleItem with the given id
throw new NotImplementedException();
}
[WebInvoke(UriTemplate = "{id}", Method = "PUT")]
public SampleItem Update(string id, SampleItem instance)
{
// TODO: Update the given instance of SampleItem in the collection
throw new NotImplementedException();
}
[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
public void Delete(string id)
{
// TODO: Remove the instance of SampleItem with the given id from the collection
throw new NotImplementedException();
}
}
}
As you can see from the code above i am throwing a WebFaultException in the GetCollection method. that should put in the body of the response a “ErrorHandler” object.
Here is how my ErrorHandler class looks like :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace WcfRestService1
{
[DataContract]
public class ErrorHandler
{
[DataMember]
public int errorCode { get; set; }
[DataMember]
public string cause { get; set; }
}
}
The crazy thing is that this thing works but it down’t :)). What i’m trying to say is that visual studio is giving me an error saying that the WebFaultException is not caught by the user code 😐 and it suspends my app. If i press continue everything works as it should.
Here are some pictures describing my problem:
First step in fiddler :
First Step
Next Visual Studio’s error: Visual Studio Error
Finally After pressing continue everything works :
It makes no sense to me and i have no idea why this thing is happening and how to fix it :P. I’ve searched the web for days trying to find a solution with no luck. I’m using Visual Studio 2010 Ultimate
Best Regards 🙂
Nothing is wrong here. You are debugging, an exception is thrown, it breaks, you continue and it works as it should.
I suspect you have set the exception handling option (Ctrl+Alt+E) to break when exceptions are thrown. (“Thrown” in the options) This will cause break whenever exception is thrown regardless it is handled.
Exceptions that are thrown in WCF operations will be handled by the WCF runtime and if they are faults, they will be sent back as such so that channel is not faulted.
Now with regard to sending back an XML, you can just send string representation of the XML using
WebFaultException<string>.