简体   繁体   中英

C# bool variable is always false/ Datetime variable is always DateTime.Min in WCF Web Service

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?

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), eg 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, eg

[DataMember(IsRequired=true)]
public DateTime EditDate { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM