【问题标题】:Why can't I send my custom class through my webservice?为什么我不能通过我的网络服务发送我的自定义类?
【发布时间】:2009-07-21 13:45:47
【问题描述】:

我有这些课程:

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

当我尝试通过我的网络服务传输 CustomGroup 时,我收到此错误:

远程服务器返回错误:NotFound

当 C# 尝试通过我的 CustomField 传输我的 StringField 时,序列化失败。

我做错了什么?

Marc Gravel 告诉我这样做,我理解解决方案,但有些事情是错误的,没有效果,出现同样的错误!! ,救命!!

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

【问题讨论】:

  • 旁注:它是长度,而不是长度。但是,我的印象是,当您使用子类时,反序列化会成为问题。不幸的是,我不记得我是如何解决它的。查看 XmlArrayItemAttribute (msdn.microsoft.com/en-us/library/…)
  • 投反对票是由于问题表述不佳/不专业,编辑已经解决了这个问题:)

标签: c# web-services serialization


【解决方案1】:

如果您将子类作为 xml 发送,则需要 [XmlInclude]:

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

您可以为模型中的任何其他子类添加多个 [XmlInclude(...)] 标记。

【讨论】:

  • 当我重新编译解决方案并尝试在客户端更新服务参考时,Marc 改变了我的情景/跨度>
  • 如果遇到同样的错误,为什么要标记为已回答?不看我不知道 System.Xml 是什么。
【解决方案2】:

List&lt;CustomField&gt; 将序列化和反序列化为 CustomField[] 如果您使用的是 Web 服务,不是吗?

【讨论】:

    【解决方案3】:

    使用

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

    改为

    【讨论】:

    • 我不清楚这在语义上是否正确。也许还有其他我们不知道(似乎很可能)包含在 FieldList 中的派生类。
    • 是的更多的树! BooleanCustomField、IntegerCustomField、ListCustomField!!!上面的示例仅解决了我的一个派生类的问题!
    • 这在 .NET 的任何版本中都无效,包括 4.0
    【解决方案4】:

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

    【讨论】:

    • 物体两边都磨损了,我没问题!! Marc Gravell 解决了我的问题!!我现在尝试解决方案!谢谢奥佩特罗夫!!
    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多