0

[Edit]: I added the TokenType enum, what caused the whole issue...

I have an issue using WCF and unfortunately I didn't find any useful help. I am creating a WCF based application. When the server responds to the client's request, I want the send back the following class:

[DataContract]
public enum TokenType
{
    User,
    Device
}

[DataContract]
public class AuthenticationResponse
{
    [DataMember]
    public LogonStatus Status { get; set; }

    [DataMember]
    public AccessToken Token { get; set; }
}

[DataContract]
public struct AccessToken
{
    [DataMember]
    public string TokenID
    {
        get;
        set;
    }

    [DataMember]
    public TokenType Type
    {
        get;
        set;
    }

    [DataMember]
    public string Uid
    {
        get;
        set;
    }

    [DataMember]
    public string Name
    {
        get;
        set;
    }

    [DataMember]
    public DateTime ExpirationTime
    {
        get;
        set;
    }

    [DataMember]
    public DateTime GenerationTime
    {
        get;
        set;
    }

    [DataMember]
    public bool IsExpired
    {
        get
        {
            return DateTime.Now > this.ExpirationTime;
        }
    }
}

When I send the AuthenticationResponse back to the client, it always fails. My qusetion: Is there any chance to use class/struct objects within DataContract object or do I have to replace the AccessToken object with basic types (e.g. string) in the AuthenticationResponse object?

Thanks all your helps! Best regards

Gabor

Gabor Varga
  • 95
  • 1
  • 11
  • What kind of "fails"? – stuartd Jun 09 '16 at 08:34
  • The data sending from server to client fails until [DataMember] public AccessToken Token { get; set; } is not null in AuthenticationResponse class. If it is null or changed with string type properties, everything works well. – Gabor Varga Jun 09 '16 at 08:39

2 Answers2

1

The problem is your public bool IsExpired has no setter and thus cause problems while serializing the object.

A workaround is to set a protected/private setter to your property with an empty body (or replace it by a method)

[DataMember]
public bool IsExpired
{
    get
    {
        return DateTime.Now > this.ExpirationTime;
    }
    set
    {
        /* Dummy setter for serialization fix */
    }
}

You can find more information about Serialization here : https://msdn.microsoft.com/en-us/library/182eeyhh.aspx

More specifically :

Items That Can Be Serialized

The following items can be serialized using the XmLSerializer class:

Public read/write properties and fields of public classes
Panda
  • 448
  • 2
  • 8
0

Ahh... Sorry for that. I was really stupid... I forgot to paste the TokenType enum in my original question what is part of AuthenticationResponse class, and this was the problem... I forget the set the [EnumMember] attributes...

After I added, everything worked well.

Sorry for this stupid and really beginner problem...

Thanks all your helps!!!

Gabor Varga
  • 95
  • 1
  • 11