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

马特乌斯·萨弗拉涅茨

这是我作为作家在 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 获得空值?

安东诺科
request.huntingGroup.directoryName = "Directory Name";

在你的代码这部分

request.huntingGroup

调用属性的getter。您需要先对其进行初始化或通过以下方式改进 getter:

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

这种方法称为“延迟初始化” -当您第一次尝试访问HuntGroup属性时,huntingGroupField将自动初始化

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在SOAP Web服务中解组为泛型类型XML中的任何类型

来自分类Dev

从泛型类返回数组

来自分类Dev

如何使用泛型返回泛型类的类类型?

来自分类Dev

黑板SOAP Web服务php

来自分类Dev

iOS中的SOAP Web服务

来自分类Dev

保护PHP Web服务(SOAP)

来自分类Dev

Soap Web服务更新屏幕在登录时返回错误

来自分类Dev

如何从 java soap web 服务返回 JSON 结果

来自分类Dev

泛型ObjectPool-如何返回泛型类?

来自分类Dev

Soap Web服务返回“内容类型为application / soap + xml; charset = UTF-8”

来自分类Dev

将自引用传递给泛型类

来自分类Dev

无法解析泛型类的服务

来自分类Dev

如何在泛型类中返回“ this”?

来自分类Dev

如何返回泛型类型的类

来自分类Dev

如何从方法返回泛型类?

来自分类Dev

从Web服务SOAP响应填充下拉列表

来自分类Dev

SOAP Web服务单元/集成测试

来自分类Dev

使用MVC的Spring Boot SOAP Web服务

来自分类Dev

使用https使用基于SOAP的Web服务

来自分类Dev

iOS 8 Swift Soap Web服务通信

来自分类Dev

用Java测试SOAP Web服务

来自分类Dev

SAS Web服务:对SOAP请求的错误响应

来自分类Dev

使用Swift使用SOAP Web服务

来自分类Dev

消费Soap Web服务时的转换问题

来自分类Dev

WSDL中的SOAP Web服务端点

来自分类Dev

在JBOSS 6.1.0上部署SOAP Web服务

来自分类Dev

cfcomponent Web服务-获取SOAP属性

来自分类Dev

使用SOAP的iOS Swift Call Web服务

来自分类Dev

PHP:SOAP Web服务无法正常工作