Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Visual Studio 2003中使用wcf webservice时遇到可为空值问题_C#_.net_Wcf_Asmx - Fatal编程技术网

C# 在Visual Studio 2003中使用wcf webservice时遇到可为空值问题

C# 在Visual Studio 2003中使用wcf webservice时遇到可为空值问题,c#,.net,wcf,asmx,C#,.net,Wcf,Asmx,我已经用VS2008编写了一个Web服务 在VS2003中添加了对该服务的引用之后,调用返回可为空值(如int? 如果我用一个值填充该字段,问题就解决了。 还有别的办法解决这个问题吗 更多信息 请看我自己代码中的这些摘录: public RevolverFund[] RetrieveRevolverFundList(int accountSetupOrganId, [System.Xml.Serialization.XmlIgnoreAttribute()] bool accountSetupO

我已经用VS2008编写了一个Web服务 在VS2003中添加了对该服务的引用之后,调用返回可为空值(如int? 如果我用一个值填充该字段,问题就解决了。 还有别的办法解决这个问题吗

更多信息 请看我自己代码中的这些摘录:

public RevolverFund[] RetrieveRevolverFundList(int accountSetupOrganId, [System.Xml.Serialization.XmlIgnoreAttribute()] bool accountSetupOrganIdSpecified) 
{
    object[] results =  this.Invoke("RetrieveRevolverFundList", new object[] 
                                                                {
                                                                      accountSetupOrganId,
                                                                      accountSetupOrganIdSpecified
                                                                });
    return ((RevolverFund[])(results[0]));
}
当我将web引用添加到解决方案时,它由VS2003自动生成。 这些代码行位于“Reference.cs”文件中。 RevolverFundView是一个具有一些可为空属性的类 无论何时调用'Invoke'方法,都会引发异常“XML文档(1481)中存在错误”

顺便说一下,这里是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; }
        }
  }

从中,当编译器box/unbox为空时,基础类型为box/unbox,而不是对象。这意味着,如果您尚未设置任何值(即HasValue属性为false),则可能会返回空值。这是一个灾难性错误-“null”不是“int”-这将引发InvalidOperationException(参见文档)

您最近是否将Web服务从
int
更改为
int?
?如果是,请在VS2003服务中更新web引用,然后重试。您应该检查VS2003实例期望的返回类型是否为
int?
而不是
int


我们有很多Web服务传递可为空的整数-在您的ResolverFund类中,'EmployeeCode'和'OrganId'可能不可为空-它们只是普通整数。使需要定义为

public int? EmployeeCode;
public int? OrganId;

公共可为空的EmployeeCode;
公共可为空的OrganId;

一旦你完成了这项工作,你就必须刷新其他项目中的web引用,然后它就可以工作了

亲爱的JBRWilkinson我完全理解你的答案。但我还不能解决这个问题。请看一些我最近添加到问题中的其他信息,以使其更清楚。谢谢你的帮助。亲爱的施赖斯·吉尔:这是我第一次使用这项网络服务。我在问题中补充了一些信息。请看它们。我不知道如何检查VS2003中的返回类型,因为当光标到达“Invoke Method”时会引发异常,正如我在代码示例中显示的那样,它们是int?在webservice的源代码处,但当我将web引用添加到我的VS2003客户端项目时,“reference.cs”文件会按照我前面所说的那样生成,如果我添加“[System.Xml.Serialization.xmlementAttribute(IsNullable=true)]”在EmployeeCode或OrganId上方,将出现另一个异常,其行为可能是这样的,因为VS2003对可空值一无所知扫描您发布了VS2008项目中的RevolverFund类定义吗?我编辑了我的问题,并在其中添加了RevolverFund定义,请参见将您的EmployeeCode和OrganId更改为可空,如我在中所做的这个答案,而不是将属性设置为nullable并重试。仅仅添加属性并不能使int在2008代码中为null-它只是告诉调用它的web服务它应该为null(这是一个谎言)。VS2003不支持可为null的类型。VS2003没有可为null的类型。
public Nullable<int> EmployeeCode;
public Nullable<int> OrganId;