问题描述
WCF客户端服务器提供列表或定制类对象数组的任何例子能帮助我但这里是我有这么远:
Any example of WCF client server providing of List or Array of custom class objects would help me! But here is what I have got so far:
下面是我的班制度我想提供
Here is my class system I want to provide
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是
where NFS is
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()
终于我做的:
and finally I do:
%svcutil% /language:cs /out:NEN_Protocol\NEN.cs http://localhost:8080/NEN_Server
它生成:
public NEN_FS.NFS[] ls()
{
return base.Channel.ls();
}
我把它在我的客户端应用程序让文件= nen.ls()和它失败:
I call it in my client application let files = nen.ls() and it fails with :
An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
Additional information: The underlying connection was closed: The connection was closed unexpectedly.
在返回base.Channel.ls();
的code线
注意是提供字符串 mmf.MMFS.Files [0]。路径;
工作得很好
Note that providing string mmf.MMFS.Files[0].Path;
works just fine
为什么呢?我究竟做错了什么? :)
Why? What am I doing wrong? :)
所有code是可以在GitHub上: https://github.com/nCdy/NENFS一>
All the code is available on GitHub : https://github.com/nCdy/NENFS
推荐答案
在我看来,该故障的原因就在这里:抽象公共类NFS
第一,可以考虑使用数据合同的使用WCF:
It seems to me, that fault reason is here: abstract public class NFS
.
The first, consider using of data contracts with WCF:
[DataContract(IsReference = true)]
abstract public class NFS : INFS
{
[DataMember]
abstract public string Path { get; set; }
// the rest of code here
}
第二,指定已知类型的为您的数据的合同。在通信信道的两侧串行器必须知道,如何seralize /反序列化的具体 NFS
'嫡系类型:
The second, specify known types for your data contract. Serializers on both side of a communication channel have to know, how to seralize/deserialize concrete NFS
' descendant type:
[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 {}
这篇关于通过WCF提供了数组或对象类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!