【问题标题】:Providing array or list of class objects via WCF通过 WCF 提供类对象的数组或列表
【发布时间】:2012-08-16 16:28:34
【问题描述】:

任何提供自定义类对象列表或数组的 WCF 客户端服务器示例都会对我有所帮助!但这是我目前所得到的:

这是我要提供的班级系统

namespace NEN_Server.FS {
    [Serializable()]
    public class XFS {
        private List<NFS> files;
        public XFS() {
            files = new List<NFS>();
            }
        public List<NFS> Files {
            get { return files; }
            set { files = value; }
            }
        }
    }

NFS 在哪里

namespace NEN_FS {
    public interface INFS : IEquatable<NFS> {
        string Path { get; set; }
        }
    [Serializable()]
    abstract public class NFS : INFS {
        abstract public string Path { get; set; }
        public NFS() {
            Path = "";
            }
        public NFS(string path) {
            Path = path;
            }
        public override bool Equals(object obj) {
            NFS other = obj as NFS;
            return (other != null) && ((IEquatable<NFS>)this).Equals(other);
            }
        bool IEquatable<NFS>.Equals(NFS other) {
            return Path.Equals(other.Path);
            }
        public override int GetHashCode() {
            return Path != null ? Path.GetHashCode() : base.GetHashCode();
            }
        }
    }

提供方法是:

namespace NEN_Server.WCF {
    public class NEN : INEN {
        private MMF mmf;
        public NEN() {
            mmf = new MMF();
            }
        public string GetRandomCustomerName() {
            return mmf.MMFS.Files[0].Path;
            }
        public NFS[] ls() {
            return mmf.MMFS.Files.ToArray();
            }

界面是

<ServiceContract>
Public Interface INEN
    <OperationContract>
    Function GetRandomCustomerName() As String
    <OperationContract()>
    Function ls() As NFS()

最后我做到了:

%svcutil% /language:cs /out:NEN_Protocol\NEN.cs http://localhost:8080/NEN_Server

它生成:

public NEN_FS.NFS[] ls()
{
    return base.Channel.ls();
}

我在我的客户端应用程序中调用它 let files = nen.ls() 并失败:

An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll

Additional information: The underlying connection was closed: The connection was closed unexpectedly.

return base.Channel.ls(); 代码行。

注意提供字符串mmf.MMFS.Files[0].Path; 工作正常

为什么?我究竟做错了什么? :)

所有代码都在 GitHub 上:https://github.com/nCdy/NENFS

【问题讨论】:

  • Q1:为什么不使用 WCF 的“原生”数据联系人? Q2:返回数组的长度是多少?
  • 对不起,我要下线3-4小时,但如果还没有人回答,我一定会看一下。快速提示 - “连接意外关闭”表明回调/服务在服务器上崩溃。在那里附加调试器或检查日志并重试,您可能会得到一些额外的信息
  • @Dennis Q1:数据联系人?我只是不知道他们。 (或者您的意思是 System.Data.Services?) Q2:它是一个元素,但它不会在 wcf 客户端中返回,因为它失败了。

标签: c# .net wcf abstraction svcutil.exe


【解决方案1】:

在我看来,故障原因在这里:abstract public class NFS.
首先,考虑在 WCF 中使用data contracts

[DataContract(IsReference = true)]
abstract public class NFS : INFS 
{
  [DataMember]
  abstract public string Path { get; set; }

  // the rest of code here
}

第二个,为您的数据合约指定known types。通信通道两侧的序列化器必须知道,如何对具体的NFS'后代类型进行序列化/反序列化:

[DataContract(IsReference = true)]
[KnownType(typeof(NFS1))]
[KnownType(typeof(NFS2))]
abstract public class NFS : INFS 
{
  [DataMember]
  abstract public string Path { get; set; }

  // the rest of code here
}

public class NFS1 : NFS {}
public class NFS2 : NFS {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    相关资源
    最近更新 更多