0

I have a Soap WS that returns an object

 [DataContract(Namespace = Configuration.Namespace)]
  public class GetAccountResponse
  {  [DataMember]
    public List<Account> accounts { get; set; }
   }
  [DataContract(Namespace = Configuration.Namespace)]
   public class Account
  { [DataMember(Name = "GUIDAccount")]
      public Guid? accountid { get; set; }
    [DataMember]
      public List<Contract> Contracts { get; set; }
   }
     [DataContract(Namespace = Configuration.Namespace)]
       public struct Contract
        {
        [DataMember(Name = "IDContrat")]
         public string contrat { get; set; }

         [DataMember(Name = "Phone")]
          public string phone { get; set; }
  }

I need to add a new attribute to the contract,but only on certain request criteria .

          [DataMember(Name = "state")]
          public string state { get; set; }

Response :

  //all the time
    return new GetAccountResponse
                    {
                        accounts = myaccounts.Values.ToList()
                    };
                    
   //my request matches a critertia the obejcts with the new 
      attribute

       if(//mycriteria)

       return new GetAccountResponse //object with state attribute
                    {
                        accounts = myaccounts.Values.ToList()
                    };

How I can achieve this by using the same objects GetAccountResponse?

nando99
  • 13
  • 4

1 Answers1

0

Maybe you can try setting the EmitDefaultValue property of the DataMemberAttribute to false. https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-member-default-values?redirectedfrom=MSDN
E.g:

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    [DataMember(EmitDefaultValue = false)]
    public string DM2;

    [DataMember]
    public string DM3;
}

Then setting this property to null:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being deserialized  
        mdc.DM2 = null;
    }

    return mdc;
}

This way, that property doesn't get written to the output stream on serialization.
source: Can I prevent a specific datamember from being deserialized?

Lan Huang
  • 613
  • 2
  • 5