【问题标题】:Silverlight localized custom validation using DataAnnotations with RIA ServicesSilverlight 使用带有 RIA 服务的 DataAnnotations 本地化自定义验证
【发布时间】:2011-06-17 12:16:29
【问题描述】:

我已经成功地使用 DataAnnotations 属性在客户端实现了本地化验证。现在,我想使用 CustomValidationAttribute 实现运行服务器端的自定义验证,但我的问题是在执行验证时我找不到获取客户端文化的方法。

这是自定义验证方法的设置:

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
}

此代码返回消息,但来自服务器当前设置的文化。

我也尝试以这种方式设置属性的属性,结果相同:

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

我还尝试在我的 DomainService 上公开一个方法来更改 ValidationResources resx 上的文化,但这似乎不仅改变了当前连接的文化,而且改变了所有连接。

由于验证是由 Ria 服务运行的,而不是我直接调用的,我如何告诉验证方法使用特定的文化?

【问题讨论】:

    标签: validation silverlight-4.0 localization data-annotations ria


    【解决方案1】:

    我遇到了this 线程,我能够解决我的问题并将区域性名称传递给 DomainContext(客户端)向服务器发出的每个请求。

    首先,我们需要创建一个自定义的 IClientMessageInspector,它负责为每个请求设置 CurrentUICulture 的参数。

    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
    }
    

    接下来,我们需要创建一个自定义 WebHttpBehavior,它将注入我们的自定义 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();
    }
    

    最后,我们扩展客户端 DomainContext.OnCreate 方法来添加我们的自定义 WebHttpBehavior注意:扩展的 DomainContext 类的命名空间必须与生成的相同...

    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();
    }
    

    现在,在服务器端,当我们想要获取语言代码时,我们可以像这样简单地访问它:

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

    要享受更多 DataAnnotation 的魔力,我们甚至可以像这样更改 DomainServiceInitialize 中的 CurrentUICulture:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      相关资源
      最近更新 更多