Search code examples
c#wcfdatacontract

WCF Multiple Base Classes in one interface?


i just wanted to know if it is possible to use more than one base class in one interface. I face the problem that i am using 2 (and there will be more) base classes for one interface. Using the first one is no problem at all, but when i try to work with the second one, it does not work.

I write down the code in short

[ServiceContract(Namespace = "http://bla.ServiceModel.Samples", SessionMode = SessionMode.Required)]
[ServiceKnownType(typeof(ObservableCollection<Models.Model1>))]
[ServiceKnownType(typeof(ObservableCollection<Models.MOdel2>))]
public interface IService
{
    [OperationContract]
    ObservableCollection<Models.Model1> AccessModel1();

    [OperationContract]
    ObservableCollection<Models.Model2> AccessModel2(string group);
}

After connecting with a client, creating a collection of Model1 works fine. When i try to create a collection of Model2, it simply crashes. The inner exception is "An existing connection was forcibly closed by the remote host."

Model1 and 2 contain different information, but have the same structure.

Is there a fundamentally mistake or something else?

If you need any further information, you are welcome!

Update

I will post the model classes. Maybe i am just blind and cant see the error.

[DataContract]
public class Model2
{
    private string status;
    private string name;
    private string telephone;

    public Model2(string sStatus, string sName, string sTelephone)
    {
        Status = sStatus;
        Name = sName;
        Telephone = sTelephone;
    }

    [DataMember(IsRequired = true, Order = 0)]
    public string Status
    {
        get { return status; }
        set { status = value; }
    }

    [DataMember(IsRequired = true, Order = 1)]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    [DataMember(IsRequired = true, Order = 2)]
    public string Telephone
    {
        get { return telephone; }
        set { telephone = value; }
    }
}

internal class Model2Builder: ObservableCollection<Model2>
{
    public Model2Builder(string group)
        : base()
    {
        try
        {
            DataTable dt = new Database().GetModel2Data(group);

            foreach (DataRow row in dt.Rows)
            {
                Add(new Model2(row[0].ToString(), row[1].ToString(), row[2].ToString()));
            }

            dt.Dispose();
        }

        catch (Exception ex)
        {
            //Code for log...
        }
    }
}

Solution

  • On your model classes have you tried adding parameterless constructors - I'm pretty sure the data contract serializer requires requires parameterless constructors, i.e.

    [DataContract]
    public class Model2
    {
        private string status;
        private string name;
        private string telephone;
    
        public Model2(){}
    
        public Model2(string sStatus, string sName, string sTelephone)
        {
            Status = sStatus;
            Name = sName;
            Telephone = sTelephone;
        }
        ...etc