how to mark an interface as DataContract in WCF

Fer picture Fer · Apr 2, 2012 · Viewed 19.7k times · Source


i have two data classes which hold only data members(no functions). One is CallTask the other is SmsTask. These two classes have some common properties like ID, Tel. I put these common properties in a seperate interface class and i use this interface class in my project whenever appropriate.
Now i added a WCFService to my project to share data between clients and server. Consider the following class design:

public interface IGsmTask : IComparable
{
    string TaskID { get; set; }
    string SessionID { get; set; }
    string Tel { get; set; }
}

class CallTask : IGsmTask
{
    #region IGsmTask Members

    public string TaskID { get; set; }

    public string SessionID { get; set; }

    public string Tel { get; set; }

    #endregion        
}

class SmsTask : IGsmTask
{
    #region IGsmTask Members

    public string TaskID { get; set; }

    public string SessionID { get; set; }

    public string Tel { get; set; }

    #endregion

    public string SmsText { get; set; }
}

in this design, i want to host CallTask, SmsTask, and IGsmTask to the clients to use these in service methots like the following;

    [OperationContract]
    public void AddTask(IGsmTask task)
    {

    }

i tried to mark [DataContract] on IGsmTask but it gives me complition error. Isnt there any methot that i can use interfaces as DataContracts? Or how should i use KnownAttributes types in this synerio?
Thanks.

Answer

daryal picture daryal · Apr 2, 2012

As far as I know using interfaces as datacontracts is not possible. You may use a base class and add knowntype attributes on the otherhand.