添加Web引用会设置清空自定义类

时间:2011-04-28 17:12:47

标签: c# .net web-services xml-serialization asmx

我的问题是我有这门课:

[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));
    }
}

该类在WebService中使用,当我添加对该服务的引用时,生成的对类(Reference.cs)的引用就是这样:

    [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 {
}

它没有任何领域,我不能使用它,为什么?更重要的是,我必须做些什么来获得一个包含我想要使用的字段的类?

PD:为了使用WebReference,我会做下一个:

        [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);
    }

当然,在构造函数中它表示“Error,该类不包含带有2个参数的构造函数。”

帮助!谢谢,当然!

1 个答案:

答案 0 :(得分:1)

当您添加网络参考时,您正在传输数据(即数据的形状),而非实施。特别是,序列化程序层正在寻找它应该在另一端代表的公共 读/写 成员(主要是属性)。

您的属性是只读的,因此无法重建(反序列化)您的对象;它会跳过这些属性。同样,自定义构造函数是 implementation ,而不是数据 - 它只需要一个公共无参数构造函数。

如果要共享实现,WCF可以使用程序集共享(在两端使用相同的DTO程序集或标识类型定义)来实现。此外,默认的WCF序列化程序(DataContractSerializer)对私有访问器感到满意,因此如果您添加了私有set(并告诉它通过DataContractAttribute / DataMemberAttribute序列化的内容),它会很高兴。

相关问题