I´m building a set of WCF services for internal 开发者_如何学JAVAuse through all our applications. For exception handling I created a default fault class so I can return treated message to the caller if its the case or a generic one when I have no clue what happened.
Fault contract:
[DataContract(Name = "DefaultFault", Namespace = "http://contoso.com/api/2010/03")]
public class DefaultFault
{
public DefaultFault(DefaultFaultItem[] items)
{
if (items == null || items.Length== 0)
{
throw new ArgumentNullException("items");
}
StringBuilder sbItems = new StringBuilder();
for (int i = 0; i
Specifying that my method can throw this exception so the consuming client will be aware of it:
[OperationContract(Name = "PlaceOrder")] [FaultContract(typeof(DefaultFault))] [WebInvoke(UriTemplate = "/orders", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST")] string PlaceOrder(Order newOrder);
Most of time we will use just .NET to .NET communication with usual binds and everything works fine since we are talking the same language. However, as you can see in the service contract declaration I have a WebInvoke attribute (and a webHttp binding) in order to be able to also talk JSON since one of our apps will be built for iPhone and this guy will talk JSON.
My problem is that whenever I throw a FaultException and have includeExceptionDetails="false" in the config file the calling client will get a generic HTTP error instead of my custom message.
I understand that this is the correct behavior when includeExceptionDetails is turned off, but I think I saw some configuration a long time ago to allow some exceptions/faults to pass through the service boundaries.
Is there such thing like this? If not, what do u suggest for my case?
Thanks a LOT!
in order to return exception you need to add above your service implementation the service behavior attribute at this format : [servicebehavior (ReturnUnknownExceptionAsFaults = true)] note that this approach shall use for debug only
liran.
No I don't think so - if the includeExceptionDetails setting is set to false, then all exceptions will be generic (and by design so). I don't know of any "loophole" to make some faults show up in detail in that case.
精彩评论