简体   繁体   English

如何在WCF服务调用中忽略区分大小写的属性名称?

[英]How to ignore case sensitive properties name in WCF service call?

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方法的可能性是什么会忽略区分大小写的属性名称(在客户端我使用JSON使用小写属性名称,但在服务器端使用大写)。 WCF can't map properties in this case. 在这种情况下,WCF无法映射属性。 Is it possible to use some WCF attributes or etc? 是否可以使用一些WCF属性等?

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

You can use the Name property of the [DataMember] attribute to map the property name: 您可以使用[DataMember]属性的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. 更新以下注释 :没有任何旋钮可用于在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. 您可以更改序列化程序以使用JSON.NET(可以这样做,请参阅此博客文章 ,但不是很容易)并使用该序列化程序中的序列化程序设置来忽略大小写。 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; } }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM