Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF DataContract vs DataContract Interface

Tags:

c#

wcf

New to WCF.

Can DataContact class inherit from Interface ?

eg:

[DataContract(Namespace = ...........)]
public class VesselSequence : IVesselSequence
{

    [DataMember]
    public int AllocationId { get; set; }

    [DataMember]
    public string ScenarioName { get; set; }
}

interface VesselSequence : IVesselSequence
{
    public int AllocationId { get; set; }
    public string ScenarioName { get; set; }
}
like image 639
Sreedhar Avatar asked Jul 07 '09 06:07

Sreedhar


2 Answers

You can do this:

[DataContract(Namespace = ...........)]
public class VesselSequence : IVesselSequence
{
    [DataMember]
    public int AllocationId { get; set; }
    [DataMember]
    public string ScenarioName { get; set; }
}

interface IVesselSequence
{
    int AllocationId { get; set; }
    string ScenarioName { get; set; }
}

You can't do this, sadly:

public class VesselSequence : IVesselSequence
{
    public int AllocationId { get; set; }
    public string ScenarioName { get; set; }
}

[DataContract(Namespace = ...........)]
interface IVesselSequence
{
    [DataMember]
    int AllocationId { get; set; }
    [DataMember]
    string ScenarioName { get; set; }
}
like image 82
JohnLBevan Avatar answered Oct 19 '22 11:10

JohnLBevan


sure it can, but keep in mind if you are returning the interface type you have to define the KnownTypes attribute for deserialization engine, so it could deserialize your sent interface at the other end.

like image 42
Martin Moser Avatar answered Oct 19 '22 12:10

Martin Moser