如何在 WCF 服务调用中忽略区分大小写的属性名称? [英] How to ignore case sensitive properties name in WCF service call?

查看:30
本文介绍了如何在 WCF 服务调用中忽略区分大小写的属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道从客户端调用 WCF 方法的可能性会忽略区分大小写的属性名称(在客户端,我使用小写属性名称使用 JSON,但在服务器端使用大写).在这种情况下,WCF 无法映射属性.是否可以使用一些 WCF 属性等?

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

推荐答案

您可以使用 [DataMember] 属性的 Name 属性来映射属性名称:

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

更新以下评论:没有任何旋钮可用于在 WCF 使用的默认序列化器上启用不区分大小写的反序列化.不过,有一些选择(都不理想).您可以更改序列化程序以使用 JSON.NET(可以这样做,请参阅 这篇博文,但不是很容易)并使用该序列化程序中的序列化程序设置来忽略大小写.我认为您还应该能够添加其他属性(可以是私有的,除非应用程序在部分信任下运行),以映射其他受支持的情况;类似于下面的代码:

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

这篇关于如何在 WCF 服务调用中忽略区分大小写的属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆