提供自定义类对象的列表或数组的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

最佳答案

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

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

关于c# - 通过WCF提供数组或类对象列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12052962/

10-12 00:01
查看更多