开发者

Silverlight localized custom validation using DataAnnotations with RIA Services

I've implemented localized validation, client-side, using the DataAnnotations attributes successfully. Now, I want to implement custom validation running server-side using the CustomValidationAttribute but my problem is that I can't find a way to get the client-side culture while executing the validation.

Here's the setup for the custom validation method开发者_如何学Go:

public static ValidationResult ValidateField( string fieldValue, ValidationContext validationContext )
{
#if !SILVERLIGHT
    // Get the message from the ValidationResources resx.
    return new ValidationResult( ValidationResources.Message, new string[]{ "Field" } );
#else
    return ValidationResult.Success;
#endif
}

This code returns the message but from the culture that the server is currently set.

I also tried to set the attribute on the property this way with same result:

      [CustomValidation( typeof( CustomValidation ), "ValidateField", ErrorMessageResourceName = "Message", ErrorMessageResourceType = typeof( ValidationResources ) )]

I also tried to expose a method on my DomainService to change the Culture on the ValidationResources resx but this seems to be changing the culture not only or the current connection but for all the connections.

Since the validation is ran by Ria Services and not something I am calling directly, how can I tell the validation method to use a specific culture?


I came across this thread and I was able to fix my issue and have the culture name pass to every request made by the DomainContext (client) to the server.

First, we need to create a custom IClientMessageInspector that will be responsible to set a parameter for the CurrentUICulture for every requests.

public class AppendLanguageMessageInspector : IClientMessageInspector
{
  #region IClientMessageInspector Members

  public void AfterReceiveReply( ref Message reply, object correlationState )
  {
    // Nothing to do
  }

  public object BeforeSendRequest( ref Message request, IClientChannel channel )
  {
    var property = request.Properties[ HttpRequestMessageProperty.Name ] as HttpRequestMessageProperty;
    if( property != null )
    {
      property.Headers[ "CultureName" ] = Thread.CurrentThread.CurrentUICulture.Name;
    }

    return null;
  }

  #endregion // IClientMessageInspector Members
}

Next, we need to create a custom WebHttpBehavior that will inject our custom IClientMessageInspector.

public class AppendLanguageHttpBehavior : WebHttpBehavior
{
  public override void ApplyClientBehavior( ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime )
  {
    clientRuntime.MessageInspectors.Add( _inspector );
  }

  private readonly AppendLanguageMessageInspector _inspector = new AppendLanguageMessageInspector();
}

Finally, we extend the client DomainContext.OnCreate method to add our custom WebHttpBehavior. NOTE: The namespace of the extended DomainContext class must be the same as the generated one...

public partial class DomainService1
{
  partial void OnCreated()
  {
    var domainClient = this.DomainClient as WebDomainClient<IDomainService1Contract>;
    if( domainClient != null )
    {
      domainClient.ChannelFactory.Endpoint.Behaviors.Add( DomainService1.AppendLanguageHttpBehavior );
    }
  }

  private static readonly AppendLanguageHttpBehavior AppendLanguageHttpBehavior = new AppendLanguageHttpBehavior();
}

Now, on the server-side, when we want to get the language code we can simply access it like this:

var cultureName = System.Web.HttpContext.Current.Request.Headers[ "CultureName" ];

To enjoy even more of the DataAnnotation magic, we can even change the CurrentUICulture in the Initialize of the DomainService like this:

public override void Initialize( DomainServiceContext context )
{
  var cultureName = System.Web.HttpContext.Current.Request.Headers[ "UICultureName" ];
  Thread.CurrentThread.CurrentUICulture = new CultureInfo( cultureName );

  base.Initialize( context );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜