2

I'm quite new to the WCF Web Services, until now I used .asmx web services, and I never had problems similar to this.

I have a class Attachement which I use to save some attachments on web site.

[DataContract]
public class Attachement
{
    private string _Name;
    [Column]
    [DataMember(Order = 1)]
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
    .
    .
    .
    private bool _IsNew;
    [DataMember(Order = 6)]
    public bool IsNew
    {
        get { return _IsNew; }
        set { _IsNew = value; }
    }...

The problem is that when I make some requests to WCF service, the field IsNew is always False, even if I'm sure that I'm sending it in request set to True. The same problem I have with a DateTime field - I send it with a value <> DateTime.Min, but in the web service I always have this field equal with DateTime.Min.

I can fix this problem using the strings instead of bool and DateTime, but it's really annoying that I cannot parse this type of variables.

What am I doing wrong?

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
Ion Gritco
  • 57
  • 1
  • 9
  • Obviously your object is not being initialized – blogbydev Sep 29 '15 at 09:38
  • 1
    Are you sure your accessing the correct variable? read http://stackoverflow.com/questions/997545/why-are-there-extra-arguments-in-my-wcf-web-service-reference – Paul Zahra Sep 29 '15 at 09:39
  • @singsuyash This is not possible. All other strings are parsed correctly, so I receive Name for example without problems. The problem is just on the property of type bool and DateTime. – Ion Gritco Sep 29 '15 at 09:40
  • 2
    @i.gritco Are you setting the populated flag correctly? http://stackoverflow.com/questions/1680356/wcf-service-proxy-not-setting-fieldspecified-property e.g. Try adding this attribute [DataMember(IsRequired=true)] – Paul Zahra Sep 29 '15 at 09:43
  • @PaulZahra thanks, you saved my day. The flag IsRequired=true solved the problem. – Ion Gritco Sep 29 '15 at 09:56

1 Answers1

5

Information gleened from here.

In WCF you must be very explicit, your parameters will have an auto generated flag parameter inserted into the method signature (look in the proxy that is generated to see them), e.g. if your field is EditDate then the autogenerated property will be EditDateSpecified, see below.

[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public System.DateTime EditDate {
    get {
        return this.editDateField;
    }
    set {
        this.editDateField = value;
        this.RaisePropertyChanged("EditDate");
    }
}

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool EditDateSpecified {
    get {
        return this.editDateFieldSpecified;
    }
    set {
        this.editDateFieldSpecified = value;
        this.RaisePropertyChanged("EditDateSpecified");
    }
}

WCF requires that you set this autogenerated flag to specify that a value is in the property that the autogenerated flag is for.

There are various methods for setting this flag, however if the field is required then there is a way to stop the 'blahblahSpecified' flag being generated by using the [DataMember(IsRequired=true)] attribute, e.g.

[DataMember(IsRequired=true)]
public DateTime EditDate { get; set; }
Community
  • 1
  • 1
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76