Skip to content

Intended Effect

Sean Mahan’s personal blog

  • About
  • When Your DataMember Custom Property Names Aren’t Working

    Let’s say you’re writing a WCF REST service, and in proper C# fashion, your object’s properties start with a capital letter. Then your service is being consumed by Objective-C code or javascript, which call for lowercase. Since WCF uses the DataContractJsonSerializer, you can set a different name for the property to serialize to:

        public class MyObject
        {
            [DataMember(Name = "name")]
            public string ObjectName { get; set; }
    

    But for some reason, it’s not working; you still end up with “ObjectName: ‘Yadda’”. I ran into this issue, and the problem turned out to be a missing line of boilerplate:

    [DataContract(Name="MyObject")]
    public class MyObject
        {
            [DataMember(Name = "name")]
            public string ObjectName { get; set; }
    

    As least as far as I’ve seen, a WCF REST service can chug along just fine without that DataContract parameter in most respects, but if you leave it out, you forego being able to set custom property names. Hope this saved you a headache!

    intendedeffect

    July 11, 2012
    Uncategorized
←Previous Page
1 … 3 4 5

Website Powered by WordPress.com.

  • Subscribe Subscribed
    • Intended Effect
    • Already have a WordPress.com account? Log in now.
    • Intended Effect
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar