PDA

View Full Version : Validating service method arguments



SonOfPirate
11-11-2009, 01:25 PM
I am new to using Spring.NET with WCF and am struggling to figure out how I can use Spring.NET validation on my service method arguments.

We use the Request/Response approach for our service methods, so a typical operation will appear as:


[OperationContract]
MyResponse SomeMethod(MyRequest request);

I'd like to add validation on the MyRequest object that is passed into my service method. Any suggestions how to do this is appreciated!

SonOfPirate
11-19-2009, 02:05 AM
Am I asking to do something that can't be done?

Bruno Baia
11-24-2009, 09:14 AM
Hi,

This is possible by applying the Validation aspect to your service.



<object id="MyService" type="MyNamespace.MyService, MyAssembly" />

<!-- Validation aspect -->
<object id="ValidationAspect" type="Spring.Aspects.Validation.ParameterValidationAdvis or, Spring.Aop"/>

<!-- Apply aspects to my services (by name) -->
<object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxy Creator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*Service</value> <!-- Aspects will be applied to all objects names that ends by 'Service' -->
</list>
</property>
<property name="InterceptorNames">
<list>
<value>ValidationAspect</value>
</list>
</property>
</object>



public MyResponse SomeMethod([Validated("SomeMethodRequestValidator")]MyRequest request)
{
...
}


- Bruno

SonOfPirate
12-08-2009, 07:41 PM
Okay, that seems to work except I am getting a standard message in the ValidationException that is thrown. I'd prefer if it would return MY message when validation fails. Here's my configuration:


<objects xmlns="http://www.springframework.net"
xmlns:v="http://www.springframework.net/validation">

<object id="localizer" type="Spring.Globalization.Localizers.ResourceSetLocaliz er, Spring.Core" />

<object id="MessageSource" type="Spring.Context.Support.ResourceSetMessageSource, Spring.Core">
<property name="ResourceManagers">
<list>
<value>MyApp.Properties.Resources, MyApp</value>
</list>
</property>
</object>

<object name="MyService"
singleton="false"
type="MyApp.MyService, MyApp" />

<v:group id="MyRequestValidator">
<v:required id="NameValidator" test="Name">
<v:message id="MyRequest_NameRequired" providers="Errors" />
</v:required>
</v:group>

<!-- Aspects -->
<object id="ValidationAdvice" type="Spring.Aspects.Validation.ParameterValidationAdvic e, Spring.Aop"/>

<object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxy Creator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>MyService</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>ValidationAdvice</value>
</list>
</property>
</object>
</objects>

I have a string resource named "MyRequest_NameRequired" in the MyApp.Properties.Resources file but this message is not used. What am I missing?

Bruno Baia
12-12-2009, 02:29 PM
Hi,

The ValidationException has a ValidationErrors property that contains your messages.


- Bruno