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; }
}
}
}
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();
}
}
}
and providing method is :
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();
}
Interface is
<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
it generates :
public NEN_FS.NFS[] ls()
{
return base.Channel.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.
on return base.Channel.ls();
line of code.
Note that providing string mmf.MMFS.Files[0].Path;
works just fine
Why? What am I doing wrong? :)
All the code is available on GitHub : https://github.com/nCdy/NENFS
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
}
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 {}
来源:https://stackoverflow.com/questions/12052962/providing-array-or-list-of-class-objects-via-wcf