IComparable not included when serializing to WCF

I have a list that I populate on the server side. This is a User list that implements IComparable. Now that WCF is serializing the data, I'm guessing this doesn't include the CompareTo method. This is my Object class:

[DataContract]
public class User : IComparable
{
    private string e164, cn, h323;
    private int id;
    private DateTime lastActive;

    [DataMember]
    public DateTime LastActive
    {
        get { return lastActive; }
        set { laatstActief = value; }
    }
    [DataMember]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    [DataMember]
    public string H323
    {
        get { return h323; }
        set { h323 = value; }
    }
    [DataMember]
    public string Cn
    {
        get { return cn; }
        set { cn = value; }
    }
    [DataMember]
    public string E164
    {
        get { return e164; }
        set { e164 = value; }
    }

    public User()
    {

    }

    public User(string e164, string cn, string h323, DateTime lastActive)
    {
        this.E164 = e164;
        this.Cn = cn;
        this.H323 = h323;
        this.LastActive= lastActive;
    }
    [DataMember]
    public string ToStringExtra
    {
        get
        {
            if (h323 != "/" && h323 != "")
                return h323 + " (" + e164 + ")";
            return e164;
        }
        set { ;}
    }

    public override string ToString()
    {
        if (Cn.Equals("Trunk Line") || Cn.Equals(""))
            if (h323.Equals(""))
                return E164;
            else
                return h323;
        return Cn;
    }

    public int CompareTo(object obj)
    {
        User user = (User)obj;
        return user.LastActive.CompareTo(this.LastActive);
    }
}

      

Can I get the CompareTo method to access the client? Putting [DataMember] is not a solution as I tried it (I know ...).

Thanks in advance.

+2


a source to share


4 answers


You don't need to implement comparable data.

You can also use delegates to sort the list, without icomparable in the class. Try the following code.



users.Sort(delegate(User u1, User u2) { return u1.LastActive.CompareTo(u2.LastActive); });

      

You can use this right in your client.

+4


a source


No, CompareTo is not a member.

If you want to replicate this on the client side, either provide a client side library that adapts the client object as well as implements IComparable.



@frogbot does have the correct suggestion, but object passing is contrary to the true nature of SOA, the purpose is to talk interfaces, which is why they made it difficult to use the NetDataContractSerializer.

+2


a source


Interfaces (code) are not serialized. You can switch to NetDataContractSerializer

. In this case, the type information will be included in the stream and you can get the same object on the client (if the assembly containing the type is in AppDomain

).

+2


a source


Since your client and server are talking about the same technology stack (i.e. both are using .Net), use the same client side code reference (data object) as the server using *. Then the interface implementations will be intact, both assemblies will use the same data object definitions, not the server using the regular definition and the client using the definition that is generated as part of the proxy.

Sharing or reusing these assemblies is a topic that has been well covered in SO.

* this means that your data objects, such as User, are contained in a separate assembly, which is the sole purpose of that assembly. Then both your client and your server (webservice) can refer to it.

+1


a source







All Articles