Bruce Hunter - 56 - Sep 17, 2014

I am throwing a ValidationException or at least the Authenticate provider is throwing it. However, the try catch block catching WebServiceException never gets called. It’s always a normal Exception and I can never catch the validation exception.

Very strange.

                using (var authService = ResolveService<AuthenticateService>())
                {
                    var response =
                        authService.Authenticate(
                            new Authenticate
                            {
                                provider = CredentialsAuthProvider.Name,
                                UserName = txtUserName.Text,
                                Password = txtPassword.Text,
                                RememberMe = false,
                            });

                    // add ASP.NET auth cookie
                    FormsAuthentication.SetAuthCookie(txtUserName.Text, true);

                   
                }
            }
            catch (WebServiceException ex)
            {
// never will get called.
            }

It’s only a WebServiceException on the client, i.e. when calling services via the ServiceClients, calling c# services in code throws normal c# exceptions.

Bruce Hunter:

I see. No reason I couldn’t easily swap out to use the Client?

Bruce Hunter:

Or do the following.

            catch (Exception ex)
            {
                if (ex is ValidationException)
                {
                    SetValidationByException(ex as ValidationException, “login”);
                }
                else
                {
                    throw;
                }
            }

That would incur the overhead of a HTTP vs a c# method call.

Bruce Hunter:

It’s funny. I was driving home and realized that the ResolveService Method hooks up a non-http call. For some reason, I thought it was making a HTTP call. Then you post that and I’m like duh.

Bruce Hunter:

I guess in some instances I’ll call the services via HTTP and some server side as well.

Hi Bruce, I’d personally recommend the more efficient option of C# services directly. You can call ex.ToResponseStatus() extension method on exceptions to convert it to the ResponseStatus DTO.

I’ve updated the WebForms project to show how you can handle Services throwing validation exception like the Register Service that’s now deployed at:
http://webforms.servicestack.net/

The code to handle the Registration form is at: https://github.com/ServiceStack/Test/blob/master/src/WebForms/WebForms/Default.aspx#L59
and the code-behind is at: https://github.com/ServiceStack/Test/blob/master/src/WebForms/WebForms/Default.aspx.cs#L69

I’m using literal controls to show the field error messages since it’s saner and more cleaner than custom validators.

Although as I try to avoid WebForms and server-side MVC as much as possible, my preference is to just call ServiceStack services directly with Ajax and use the auto-binding errors feature as described in: https://github.com/ServiceStackApps/EmailContacts#fluent-validation