【问题标题】:Why can't I pass this object as an argument?为什么我不能将此对象作为参数传递?
【发布时间】:2013-05-07 22:11:57
【问题描述】:

背景:

使用 .NET 4.0 在 C# 中编写使用 Web 服务的客户端。代理类包括一个记录类型的对象,该对象由来自 Web 服务的 SOAP 响应填充。

情况:

API 包含两个函数,findRecordsgetRecords。这两个函数都下载实际记录,不同之处在于findRecords 是 void 类型并通过 out 参数提供记录,而 getRecords 是记录类型并因此返回记录。

问题:

执行对findRecords 的调用后,我可以访问记录对象的成员(如recordIDrecordTitle 等)以用于其他函数参数。但是,如果我尝试将记录对象本身作为参数传递,我会得到一个ArgumentNullException。我目前可以将记录作为参数传递的唯一方法是使用从getRecords 函数返回的记录。这种方法的缺点是它会使我需要进行的 API 调用数量增加一倍,这会降低我的客户端和 Web 服务的速度。

问题:

为什么会这样,我可以做些什么来将来自findRecords 的记录对象作为参数传递?

代码:

这是 findRecords 函数的定义:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/findRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public void findRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] filter filter, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")] string offset, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")] string count, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] out System.Nullable<int> numResults, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlIgnoreAttribute()] out bool numResultsSpecified, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] out record[] results) {
        object[] results1 = this.Invoke("findRecords", new object[] {
                    username,
                    filter,
                    offset,
                    count});
        numResults = ((System.Nullable<int>)(results1[0]));
        numResultsSpecified = ((bool)(results1[1]));
        results = ((record[])(results1[2]));
    }

getRecords函数的定义:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/getRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlArrayAttribute("records", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    [return: System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
    public record[] getRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=false)] string[] stiIds) {
        object[] results = this.Invoke("getRecords", new object[] {
                    username,
                    recordIds});
        return ((record[])(results[0]));
    }

我想用它做什么:

        // Objects to use for the findRecords call
        int? numResults;
        bool numResultsSpecified;
        record[] records;

        // Object for handling and saving the XML
        XRecord r;

                try
                {
                    ws.findRecords(usernames[uname_Index], GetFilter(), offset.ToString(), count.ToString(),
                        out numResults, out numResultsSpecified, out returnRecords);

                    for (int i = 0; i < returnRecords.Length; i++)
                    {
                        count--;
                        r = GrabRecord(returnRecords[i]);
                        r.record.Save(@".\Records\" + r.id + "_" + r.date2 + ".xml");
                    }
                }

......

    private static XRecord GrabRecord(record _record)
    {
        XNamespace nameSpace = "http://www.<redacted>.com/ws/schemas";

        XDocument r =
            new XDocument(
                new XElement(nameSpace + "getRecordsResponse",
                    new XAttribute(XNamespace.Xmlns + "ns1", nameSpace),
                    new XElement("list",
                        new XElement("ID", _record.id),
                        new XElement("title", _record.title),
            ...............
                        new XElement("comments", _record.comments),
                        new XElement("category", _record.category),
                        _record.modifiedDateSpecified ? new XElement("modifiedDate", _record.modifiedDate) : null,
                        new XElement("attachments", from a in _record.attachments
                                                    select new XElement("list",
                                                        new XElement("id", a.id),
                                                        new XElement("filePath", a.filePath),
                                                        new XElement("type", a.type))));

        XRecord xr = new XRecord();
        xr.record = r;
        xr.id = _record.id;
        xr.date2 = ConvertToDateString(_record.modifiedDate);

        return xr;
    }

这是异常和堆栈跟踪信息。引用的行号是指相应函数中的“XDocument r =”和“r = GrabRecord(...)”行。

Unexpected error: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable1 来源,Func2 selector) at WsClient.WSAPI.GrabRecord(record _record) in C:\...WSAPI.cs:line 1235 at WsClient.WSAPI.PersistentUpdate(String[] usernames) in C:\...WSAPI.cs:line 354

按照 rsbarro 的建议,我编辑了 GrabRecord 函数中的代码,以消除 ConvertToDateString() 在 modifiedDate 为空时可能导致问题的可能性。这并没有消除问题,并且异常消息没有改变。

【问题讨论】:

  • 你能粘贴一些代码来说明你在说什么吗?
  • 当然,尤其是您所指的不可通过的 object。此外,请务必包含引发异常的函数的代码。
  • 我们需要看代码。您是否在某处以某种(错误)方式使用 as 关键字来获取 null 引用而不是对您的实例的引用?
  • 按要求添加代码。
  • 自从我发布此内容以来,我尝试了以下操作:将record[] 类型的静态成员添加到我的类,然后将该成员初始化为count 参数的大小在 findRecords 中使用。每次我得到同样的错误。我过去也使用过 foreach 循环而不是迭代器循环,也没有区别。

标签: c# web-services function proxy-classes


【解决方案1】:

通过查看代码,我猜您在调用GrabRecord 时收到了ArgumentNullExceptionGrabRecord 中存在可能导致ArgumentNullException 的潜在问题。当您构建要存储在 r 中的 XDocument 时,请在创建 modifiedDate XElement 之前检查 _record.modifiedDateSpecified。但是,在方法结束时,执行以下语句之前没有任何 null 检查:

xr.date2 = ConvertToDateString(_record.modifiedDate);

_record.modifiedDate 可能为空,而ConvertToDateString 正在启动ArgumentNullException。当然ConvertToDateString 有可能处理空值而不出错,但是如果没有看到该代码就很难说。

如果问题不是我所建议的,您能否更新问题,提供有关异常的更多详细信息,并添加堆栈跟踪?

希望对您有所帮助。

【讨论】:

  • 我明白你所说的关于那里的空引用可能性。不幸的是,我添加了代码来解决这个问题,但仍然遇到同样的错误。我会将异常和堆栈跟踪添加到主帖中。
  • Hrm,您使用findRecords获取记录时,_record.attachments是否可能为空?
  • 我想你可能已经找到了。我使用 SoapUI 来帮助对 API 调用进行建模,因此我使用相同的记录 ID 执行了对 getRecords 的调用和对 findRecords 的调用。前者返回附件列表,而后者将其排除。我现在应该可以解决这个问题了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-12-17
  • 2016-08-16
  • 2014-03-12
  • 1970-01-01
  • 2021-10-10
  • 2021-04-25
  • 2018-09-12
相关资源
最近更新 更多