简体   繁体   中英

Why can't I send my custom class through my webservice?

I have these classes:

public abstract class CustomField
{
    public String Id { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public FieldType Type { get; set; } 

    public enum FieldType
    { 
        String = 0,
        Integer = 1,
        Boolean = 2,
        List = 3
    }
}

public class StringCustomField:CustomField
{
    public String Value { get; set; }
    public Int32 MinLenght { get; set; }
    public Int32 MaxLenght { get; set; }

    public StringCustomField()
    {
        this.Type = FieldType.String;
    }
}

public class CustomGroup
{
    public String Id { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public List<CustomField> FieldList = new List<CustomField>();
}

When I try to transfer CustomGroup through my webservice I get this error:

The remote server returned an error: NotFound

Serialization is failing when C# tries to transfer my StringField through my CustomField .

What am I doing wrong?

Marc Gravel tell me to do that and i understand the solution but some thing is wrong, no effects, cath the same error!! , help!!

[XmlInclude(typeof(StringCustomField))]
[XmlInclude(typeof(IntegerCustomField))]
[XmlInclude(typeof(BooleanCustomField))]
[XmlInclude(typeof(ListCustomField))]
public abstract class CustomField
{
    public String Id { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public FieldType Type { get; set; } 

    public enum FieldType
    { 
        String = 0,
        Integer = 1,
        Boolean = 2,
        List = 3
    }
}

如果您使用的是Web服务, List<CustomField>将序列化并反序列化为CustomField[] ,不是吗?

use

public class CustomGroup
{
    public String Id { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public List<CustomField> FieldList = new List< StringCustomField >();

}

instead

If you are sending subclasses as xml, you will need [XmlInclude] :

[XmlInclude(typeof(StringCustomField))]
public abstract class CustomField
{...}

You can add multiple [XmlInclude(...)] markers for any other subclasses in the model.

如果我理解正确,则应该1.将Web服务连接到应用程序2.使用WS的名称空间,因此所有类都将从Proxy中使用,我认为本地类不会被Proxy理解。远程网络正确服务,即使您在双方上使用相同的程序集

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM