c# - 通过 WCF 提供类对象的数组或列表

标签 c# .net wcf abstraction svcutil.exe

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

这是我想提供的类(class)系统

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用于您的数据契约(Contract)。通信 channel 两侧的序列化器必须知道如何序列化/反序列化具体的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/

相关文章:

c# - 寻找位置 API (c#)

c# - 将 "Quartz Scheduler .NET"移植到 Windows Azure -> Web 应用程序。不适用于 Windows Azure VM

c# - 字符串 (";P") 更大还是字符串 ("-_-") 更大?

c# - SslStream.AuthenticateAsServer 证书链

c# - Application Insights - 服务器没有显示任何数据

c# - 在 Windows Server 2008 R2 和 IIS7.5 中托管 WCF

c# - 断开/关闭客户端

.net - DataGridView 最后一列标题后的空白

c# - 如果类具有 JsonExtensionData,如何使用 Json.NET 订购属性?

silverlight - 契约(Contract)需要双工,但绑定(bind) 'BasicHttpBinding' 不支持或未正确配置以支持它