1

I'm trying to make a WCF service that will be consumed by other parties by passing a SOAP request to the service. The client requires me to handle a set of FaultExceptions that could happen during the interaction.

This includes that if the client send a malformed Uri in the SOAP request [wsa:To] element, that is if the client for example send a request contains the following:

<wsa:To>http//:this.is.invalid/address</wsa:To>

I should be able to throw a specific FaultException. I tried to implement the IDispatchMessageInspector to capture the SOAP request before it reach the operation but when the client send a request contains a bad Uri like in the above example, the AfterReceiveRequest is not called and so I can NOT handle this type of error.

I couldn't find the error until I've enabled the trace logging for my WCF service and I'm the error

System.UriFormatException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089<br><br>with the description <br><br>Handling an exception. Exception details: System.UriFormatException: Invalid URI: The URI scheme is not valid.

The question is how and where can I catch this exception in the Code?

Thanks in advance,

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Abollo
  • 66
  • 7
  • Note: On the client side, I receive a 400 HTTP error as a web exception without any details. – Abollo Jan 02 '14 at 14:35

1 Answers1

0

The soap header wsa:To should contain the address of the intended receiver of the message. WCF client-side framework enforces this by using the service endpoint address as the value for the wsa:To header element. I'm not sure whether the WCF service-side framework enforces validation of this element since it has already ready received the soap message.

You're seeing the 400 HTTP error because the WCF client is attempting to sent a soap message to an invalid service endpoint. To properly test an invalid wsa:To value you need code that will create the soap XML with an invalid wsa:To value but sent to the correct service endpoint.

If WCF doesn't validate this value, you're approach of using an implementation of the IDispatchMessageInspector should work. Otherwise, if WCF throws an exception before the IDispatchMessageInspector implementation is called then you may need to implement a IErrorHandler endpoint behavior to catch this kind of "in the plumbing" exception. See this great blog post for information on how to implement and configure it.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • Thanks for your reply, but I had already made the client send a good formatted header with wrong value to my WCF service like `http:\\any.thing\address` and the AfterReceiveRequest method was hit by the debugger and there I can throw the required exception. but when the format is wrong, the AfterReceiveRequest method is not hit at all. regading the IErrorHandler, I'll check it and get back to you again. thanks again – Abollo Jan 02 '14 at 16:38