Trapping Template Errors
When using a typed contract with URI templates, how do I catch any type conversion errors?
The WebGet and WebInvoke attributes allow you to specify a URI template that maps components of the request address to operation parameters. For example, the URI template "weather/{state}/{city}" maps the request address into literal tokens, a parameter named state, and a parameter named city. These parameters might then be bound by name to some of the formal parameters of the operation.
The template parameters appear in the form of strings within the URI while the formal parameters of a typed contract might be of non-string types. Therefore, a conversion needs to take place before the operation can be invoked. A table of conversion formats is available if you're curious about the supported types of conversions. However, if the conversion fails, then there's no way to let the operation know that something has gone wrong. The type conversion instead surfaces in the form of a dispatch failure.
You can inspect a type conversion dispatch failure the same as with many other types of dispatch failures by installing an error handler. The IErrorHandler extensibility point is called to handle service-level exceptions. From there you can choose the response that you'd like to have sent back to the caller as well as perform any custom tasks and reporting that you'd like.