对通用类的SOAP Web服务引用返回nullexception

时间:2017-09-05 11:16:06

标签: c# web-services generics soap

这是我作为作家在stackoverflow上的首次亮相。我堆积......

我有Windows窗体应用程序。我将ServiceReference添加到.wsdl Web服务。一切都很好但我在初始化泛型类的字段时遇到了问题。

本课程如下所示:

public partial class setHuntingGroupRequest : object, System.ComponentModel.INotifyPropertyChanged {

    private string fwSessionIdField;

    private string pbxNameField;

    private AlcHuntingGroup huntingGroupField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)]
    public string fwSessionId {
        get {
            return this.fwSessionIdField;
        }
        set {
            this.fwSessionIdField = value;
            this.RaisePropertyChanged("fwSessionId");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=1)]
    public string pbxName {
        get {
            return this.pbxNameField;
        }
        set {
            this.pbxNameField = value;
            this.RaisePropertyChanged("pbxName");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)]
    public AlcHuntingGroup huntingGroup {
        get {
            return this.huntingGroupField;
        }
        set {
            this.huntingGroupField = value;
            this.RaisePropertyChanged("huntingGroup");
        }
    }

所以我有fwSessionId,pbxName和嵌套的AlcHuntingGroup看起来:

   public partial class AlcHuntingGroup : object, System.ComponentModel.INotifyPropertyChanged {

        private string directoryNumberField;

        private string directoryNameField;

        private AlcHuntingGroupSearchType searchTypeField;

        private string[] membersField;

        private bool unavailableAuthorizedField;

        private bool releaseAfterTimerField;

        private string overflowDirectoryNumberField;

        private int entityField;

(...)

我的代码如何工作:

        private void btnDodajnrdohg_Click(object sender, EventArgs e)
        {

        SoapDemo.ServiceReference1.AlcPbxManagementPortTypeClient soap = new 
        SoapDemo.ServiceReference1.AlcPbxManagementPortTypeClient();

        setHuntingGroupRequest request = new setHuntingGroupRequest();
        setHuntingGroupResponse response = new setHuntingGroupResponse();

        request.fwSessionId = session_id;
        request.pbxName = "demooxemai42";

        try
            {

            response = soap.setHuntingGroup(request);

            this.output.Text = response.resultCode.ToString();
            }
            catch (Exception error)
            {

            this.output.Text = "Error in request: " + error + "\n";
            }
          }

在上面的例子中,我插入请求两个字段。不幸的是,我还必须插入AlcHuntingGroup类的所有字段(huntingGroup是对此类的引用)。我试过这个:

request.fwSessionId = session_id;
request.pbxName = "demooxemain2";
request.huntingGroup.directoryName = "Directory Name";
request.huntingGroup.directoryNumber = "1001";
request.huntingGroup.entity = 1;
//etc

Intellisense正确地看到了这个字段,但是当我开始调试这段代码时,我会在request.huntingGroup.directoryName = "Directory Name";

中返回错误。

System.NullReferenceException:对象引用未设置为对象的实例 SoapDemo.ServiceReference1.setHuntingGroupRequest.huntingGroup.get返回null。

如何防止来自huntingGroup的null值?

1 个答案:

答案 0 :(得分:0)

request.huntingGroup.directoryName = "Directory Name";

关于你的代码这部分

request.huntingGroup

调用财产的getter。您需要先将其初始化或通过以下方式改进getter:

get {
        return this.huntingGroupField ?? (this.huntingGroupField = new AlcHuntingGroup());
    }

这种方法称为&#34;延迟初始化&#34; - 当您第一次尝试访问 huntingGroup 属性时,会自动初始化 huntingGroupField