Reputation: 51
I am having issues with using the Enums in WCF. As soon as it returned the object with enum value, I got an exception. I did a lot of search and found numerous questions related to the problem I have. I did follow those solutions but is not working. (I changed those Enum type to int and it worked. I got an exception as soon as I changed it to the Enum. I believe that the Enum is causing the problem). Please help.
I have the following:
[DataContract]
public enum UserRoleEnum:int
{
[EnumMember]
SystemAdmin = 1,
[EnumMember]
Waiter = 2
}
[DataContract()]
public partial class UserInfoDTO
{
[DataMember()]
public Int32 UserID { get; set; }
[DataMember()]
public String UserName { get; set; }
[DataMember()]
public byte[] Password { get; set; }
[DataMember()]
public String UserStatusName { get; set; }
[DataMember()]
public UserRoleEnum UserRole { get; set; }
.
.
.
}
In my service contact,
[ServiceContract(SessionMode=SessionMode.Allowed)]
[ServiceKnownType(typeof(UserRoleEnum))]
public interface IUserService
{
[OperationContract()]
UserDTO[] GetUser(int userID);
}
I am getting the following error (which I concluded that Enum was causing it)
{"The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9759247'."}
Upvotes: 0
Views: 825
Reputation: 51
I solved this by making the enums to start at 0 instead of 1.
[DataContract]
public enum UserRoleEnum:int
{
[EnumMember]
SystemAdmin = 0,
[EnumMember]
Waiter = 1
}
and I tried not setting any values for enum as below:
[DataContract]
public enum UserRoleEnum:int
{
[EnumMember]
SystemAdmin,
[EnumMember]
Waiter
}
Both of them work. But I found a post that setting enums to 0 does not work (see the link) http://haishibai.blogspot.com/2010/12/quick-tip-enum-types-in-wcf.html
Can anyone explain or confirm that enums to be started at 0 (instead of 1 in WCF) and why my results are inconsistent with the post (maybe the blogger is incorrect)?
Upvotes: 1
Reputation: 51
Can you try adding [Serializable] above the [DataContract] in your Enum? Might be having trouble with serialization I'm not sure though. Please update with the result.
Upvotes: 1