Take the 2-minute tour ×

A dumb question: Let's say I have an object that i store in viewstate. I have it marked as Serializable. I also reuse the same object in my WCF service. But the web service serializes and exposes some internal properties that are not necessary/safe to display to a service client. Is there a way to allow field/property serialization for ViewState but not Web Service? [NonSerialized] hides properties from both. I know I can implement a POCO for web service use, but I wanted to keep method/object signatures unchanged as far as the class names. Just wonder if there's a way. If not, i'm gonna have to either live without those fields in ViewState or create POCOs and let customers worry about reimplementing their clients.


share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Yes, when creating a DataContract for a web service, you mark the members you want to be included in the service requests and replies with the [DataMember] attribute.

public class MyData
    private int id_value;

    // Apply the DataMemberAttribute to the property.
    public int ID

        get { return id_value; }
        set { id_value = value; }

    public int DontExposeMeToWcf { get; set; }

Edit: In .NET 4.0 you can also use the [IgnoreDataMember] Attribute to exclude a member from serialization. From Using Data Contracts:

By default, the DataContractSerializer infers the data contract and serializes all publicly visible types. All public read/write properties and fields of the type are serialized. You can opt out members from serialization by using the IgnoreDataMemberAttribute.

The IgnoreDataMemberAttribute attribute is only honored when used with unmarked types. This includes types that are not marked with one of the DataContractAttribute, SerializableAttribute, CollectionDataContractAttribute, or EnumMemberAttribute attributes, or marked as serializable by any other means (such as IXmlSerializable).

share|improve this answer
As far as i know, you don't need to decorate objects/properties with DataContract/DataMember in .NET 4 in order to serialize them through WCF. I know for sure these properties that i'm trying to hide are not marked as DataMembers and they are still serialized. –  Dimitri Jul 27 '11 at 13:15
@Dimitri it is still best practice to include them. –  Marc Gravell Jul 27 '11 at 13:18
Okay, my scenario is: I have a class MyClass that is marked as DataContract and has properties marked as DataMember. Some of the properties of this class are not marked as DataMember and they are NOT being serialized. But this class inherits from another class MyBaseClass that is marked Serializable, but not DataContract. The properties of this base class show up in serialization. So if i decorate the base class as DataContract and not decorate its properties as DataMembers they shouldn't Serialize to web service? –  Dimitri Jul 27 '11 at 13:21
@Dimitri: See my edit. You can use either [DataContract] and [DataMember] on all fields that you want serialized (for opt in), or you can use [IgnoreDataMember] on fields that you don't want serialized for opt-out. –  Ryan Gross Jul 27 '11 at 13:23
Cool, thanks a bunch. I did try IgnoreDataMember but i guess i did something wrong. Now i marked the base class as DataContract but didn't mark any of its properties as DataMember and they didn't go over. –  Dimitri Jul 27 '11 at 13:26
show 2 more comments

Your Answer


