1

My problem is that I have this class:

[SerializableAttribute]
[SoapInclude(typeof(PairColumnValue))]
[XmlInclude(typeof(PairColumnValue))]
public class PairColumnValue:IPairColumnValue
{
    public string ColumnName
    {
        get { return data.Element("ColumnName").Value; }
    }
    public string Value
    {
        get { return data.Element("Value").Value; }
    }


    private XElement data;

    public PairColumnValue()
    {
        data = new XElement("Data", new XElement("ColumnName", ""), new XElement(("Value"), ""));
    }

    public PairColumnValue(string columnName, string value)
    {
        data = new XElement("Data", new XElement("ColumnName", columnName), new XElement(("Value"), value));
    }
}

That class is used in a WebService, and when I add a reference to the service, the generated reference to the class (Reference.cs) is as it goes:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class PairColumnValue {
}

It doesn't have any field, and I cannot use it, why? And more important, what must I do to obtain a class with the fields that I want to use?

PD: in order to use the WebReference, I do the next:

        [Test]
    public void InsertThroughService()
    {
        ServiceReference.PairColumnValue[] toInsert = new ServiceReference.PairColumnValue[2];

        toInsert[0] = new PruebasUnitarias.ServiceReference.PairColumnValue("login", "testservice");

        toInsert[1] = new ServiceReference.PairColumnValue("password", "testservice");

        ServiceReference.PersistenceServicev2 client = new PersistenceServicev2();

        XmlSerializer serializer = new XmlSerializer(typeof(PairColumnValue));


        client.InsertData("usuario", toInsert);
    }

And, of course, at the constructors it says "Error, the class doesn't contain a constructor with 2 arguments.

Help! And thanks, for sure!

1 Answer 1

1

When you add a web-reference you are transporting data (i.e. the data's shape), not implementation. In particular, the serializer layer is looking for public read/write members (mainly properties) that it should represent at the other end.

Your properties are read-only, so there is no way it can reconstruct (deserialize) your object; it skips those properties. Likewise, a custom constructor is implementation, not data - it only really wants a public parameterless constructor.

If you want to share implementation, WCF can do that with assembly sharing (you use the same DTO assembly at both ends, or identicial type definitions). Additionally the default WCF serializer (DataContractSerializer) is happy with private accessors, so it would be happy if you added a private set (and told it what to serialize via DataContractAttribute/DataMemberAttribute).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.