dimanche 4 mai 2014

c# - Client WCF : en passant une valeur enum toujours obtient 0 - Stack Overflow


I have the following code written in the service.


[DataContract]
public class Email
{
private string senderEmail;
private MyEmailType emailType;

[DataMember]
public string SenderEmail { get { return senderEmail; } set { senderEmail = value; } }
[DataMember]
public MyEmailType EmailType { get { return emailType; } set { emailType = value; } }
}

[DataContract]
public enum MyEmailType
{
[EnumMember]
EmailType1 = 1,
[EnumMember]
EmailType2= 2
}

I send Email objects to the service like this:


Email email = new Email();
email.SenderEmail = "someemail@mail.com";
email.EmailType = MyEmailType.EmailType1;

bool isDispatchSuccessful = myClientObject.SendMassProcessEmail(email);

When I get to the service, the SenderEmail property's value is correct, but the EmailType (the enum one) gets 0, instead of 1 (EmailType1).


Why is this happening?


Edit:


I tried passing a MyEmailType variable besides the Email object to the SendMassProcessEmail like this:


bool isDispatchSuccessful = myClientObject.SendMassProcessEmail(email, MyEmailType.EmailType1);

Now, the stand-alone enum variable receives a correct value, but the one which is a public member of the Email class still gets a value of 0.




To answer the question ill refer you to this link http://msdn.microsoft.com/en-us/library/aa347875(v=vs.110).aspx


If you really want to pass the actual value i think you should do it like this:


[DataContract]
public enum MyEmailType
{
[EnumMember(Value = 1)]
EmailType1 = 1,
[EnumMember(Value = 2)]
EmailType2= 2
}



You have to add the [DataContract] to your backing fields:


[DataContract]
public class Email
{
[DataMember]
private string senderEmail;

[DataMember]
private MyEmailType emailType;

public string SenderEmail { get { return senderEmail; } set { senderEmail = value; } }
public MyEmailType EmailType { get { return emailType; } set { emailType = value; } }
}


I have the following code written in the service.


[DataContract]
public class Email
{
private string senderEmail;
private MyEmailType emailType;

[DataMember]
public string SenderEmail { get { return senderEmail; } set { senderEmail = value; } }
[DataMember]
public MyEmailType EmailType { get { return emailType; } set { emailType = value; } }
}

[DataContract]
public enum MyEmailType
{
[EnumMember]
EmailType1 = 1,
[EnumMember]
EmailType2= 2
}

I send Email objects to the service like this:


Email email = new Email();
email.SenderEmail = "someemail@mail.com";
email.EmailType = MyEmailType.EmailType1;

bool isDispatchSuccessful = myClientObject.SendMassProcessEmail(email);

When I get to the service, the SenderEmail property's value is correct, but the EmailType (the enum one) gets 0, instead of 1 (EmailType1).


Why is this happening?


Edit:


I tried passing a MyEmailType variable besides the Email object to the SendMassProcessEmail like this:


bool isDispatchSuccessful = myClientObject.SendMassProcessEmail(email, MyEmailType.EmailType1);

Now, the stand-alone enum variable receives a correct value, but the one which is a public member of the Email class still gets a value of 0.



To answer the question ill refer you to this link http://msdn.microsoft.com/en-us/library/aa347875(v=vs.110).aspx


If you really want to pass the actual value i think you should do it like this:


[DataContract]
public enum MyEmailType
{
[EnumMember(Value = 1)]
EmailType1 = 1,
[EnumMember(Value = 2)]
EmailType2= 2
}


You have to add the [DataContract] to your backing fields:


[DataContract]
public class Email
{
[DataMember]
private string senderEmail;

[DataMember]
private MyEmailType emailType;

public string SenderEmail { get { return senderEmail; } set { senderEmail = value; } }
public MyEmailType EmailType { get { return emailType; } set { emailType = value; } }
}

0 commentaires:

Enregistrer un commentaire