Javascript – How to ignore case sensitive properties name in WCF service call

cjavascriptwcf

Hello I wonder about possibility to call WCF method from client side what would be ignore case sensitive properties names (on client side I am working with JSON with lowercase properties name, but on server side with uppercase). WCF can't map properties in this case.
Is it possible to use some WCF attributes or etc?

 public interface IMyWCF
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        bool UpdateUser(User user);

}
    [Serializable]
    [DataContract]
    public class User : ICloneable  
    {
        [DataMember]
        [JsonProperty(PropertyName = "login")]
        [StringLength(40, ErrorMessage = "The Login value cannot exceed 40 characters. ")]
        [DefaultValue("")]
        public String Login { get; set; }

        [DataMember]
        [JsonProperty(PropertyName = "id")]
        public int UserId { get; set; }
 }

Best Answer

You can use the Name property of the [DataMember] attribute to map the property name:

[DataContract]
public class User : ICloneable  
{
    [DataMember(Name = "login")]
    [JsonProperty(PropertyName = "login")]
    [StringLength(40, ErrorMessage = "The Login value cannot exceed 40 characters. ")]
    [DefaultValue("")]
    public String Login { get; set; }

    [DataMember(Name = "id")]
    [JsonProperty(PropertyName = "id")]
    public int UserId { get; set; }
}

Update following comment: There isn't any knob you can use to enable case-insensitive deserialization on the default serializer used by WCF. There are some options (none ideal), though. You can change the serializer to use JSON.NET (which can be done, see this blog post, but not very easily) and use the serializer settings in that serializer to ignore casing. I think you should also be able to add additional properties (which can be private, except if the application is running in partial trust), to map the additional supported cases; something similar to the code below:

[DataContract]
public class User
{
    [DataMember]
    public String Login { get; set; }
    [DataMember]
    private String login { get { return this.Login; } set { this.Login = value; } }

    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    private int id { get { return this.UserId; } set { this.UserId = value; } }
}