Mehdi.KH
Mehdi.KH

Reputation: 7

Having problem with nullable values using wcf webservice in Visual Studio 2003

I have Written a webservice with VS2008 after I had Added Reference to that service in VS2003,I encountered a problem calling methods which return nullable values such as int? if I fill that feild with a value,the problem solves. Is There any other way to solve this problem?

Some more Information

Please Look at these extract from my own code:

public RevolverFund[] RetrieveRevolverFundList(int accountSetupOrganId, [System.Xml.Serialization.XmlIgnoreAttribute()] bool accountSetupOrganIdSpecified) 
{
    object[] results =  this.Invoke("RetrieveRevolverFundList", new object[] 
                                                                {
                                                                      accountSetupOrganId,
                                                                      accountSetupOrganIdSpecified
                                                                });
    return ((RevolverFund[])(results[0]));
}

It is generated by VS2003 automatically when I add the web reference to my solution. these lines of code located in 'Reference.cs' file. RevolverFundView is a class that has some nullable properties the exception "There is an error in XML document (1, 481)." is thrown whenever the 'Invoke' Method is called.

by the way here is declaration of RevolverFundClass

public class RevolverFund {
    
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string Comment;
    
    public int EmployeeCode;
    
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool EmployeeCodeSpecified;
    
    public int Id;
    
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool IdSpecified;
    
    public int OrganId;
    
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool OrganIdSpecified;
}

in 'RevolverFund' class , 'EmployeeCode' and 'OrganId' properties are declared as 'Nullable Int's

**and here is The RevolverFund class difinition In VS2008**


    [DataContract]
    public class RevolverFund
    {
        private Int32 m_Id;
        [DataMember]
        public Int32 Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }
        
        private Int32? m_EmployeeCode;
        [DataMember]
        public Int32? EmployeeCode
        {
            get { return m_EmployeeCode; }
            set { m_EmployeeCode = value; }
        }
       
        private Int32? m_OrganId;
        [DataMember]
        public Int32? OrganId
        {
            get { return m_OrganId; }
            set { m_OrganId = value; }
        }
       
        private String m_Comment;
        [DataMember]
        public String Comment
        {
            get { return m_Comment; }
            set { m_Comment = value; }
        }
  }

Upvotes: 0

Views: 4461

Answers (3)

Chris Gill
Chris Gill

Reputation: 2928

In your ResolverFund class, 'EmployeeCode' and 'OrganId' are not nullable - they are just normal ints. To make them nullable that need to be defined as

public int? EmployeeCode;
public int? OrganId;

or

public Nullable<int> EmployeeCode;
public Nullable<int> OrganId;

Once you have done that you must refresh your web references from your other project, then it should work

Upvotes: 0

Chris Gill
Chris Gill

Reputation: 2928

Did you change your webservice from int to int? recently? If so, update your web references in your VS2003 service, then try again. You should check that the return type your VS2003 instance is expecting is int? rather than int

We have plenty of webservices that pass round nullable ints - it is possible

Upvotes: 0

JBRWilkinson
JBRWilkinson

Reputation: 4875

From the Documentation on MSDN, when the compiler boxes/unboxes Nullable, the underlying type is boxed/unboxed, rather than the object. This means that if you haven't set any value yet (i.e. HasValue property is false) then you'd potentially get a null value returned. That's a catastrophic error - 'null' is not an 'int' - which will throw an InvalidOperationException (see docs)

Upvotes: 1

Related Questions