c# - 为什么我不能将此对象作为参数传递?

标签 c# web-services function proxy-classes

背景:

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

情况:

该 API 包括两个函数:findRecordsgetRecords。这两个函数都下载实际记录,区别在于 findRecords 的类型为 void,并通过 out 参数提供记录,而 getRecords 的类型为 record,因此返回记录。

问题:

执行对 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(...)”行。

意外错误:值不能为空。 参数名称:来源 在 System.Linq.Enumerable.Select[TSource,TResult](IEnumerable1 源,Func2 选择器) 在 C:\...WSAPI.cs 中的 WsClient.WSAPI.GrabRecord(record _record) 处:第 1235 行 在 C:\...WSAPI.cs 中的 WsClient.WSAPI.PersistentUpdate(String[] usernames) 处:第 354 行

正如 rsbarro 所建议的,我编辑了 GrabRecord 函数中的代码,以消除如果 modifiedDate 为 null 则 ConvertToDateString() 可能导致问题的可能性。这并没有消除问题,并且异常消息也没有改变。

最佳答案

通过查看代码,我猜测您在调用 GrabRecord 时遇到了 ArgumentNullExceptionGrabRecord 中存在一个潜在问题,可能会导致 ArgumentNullException。当您构建要存储在 r 中的 XDocument 时,请在创建 modifiedDate 之前检查 _record.modifiedDateSpecified XElement。但是,在该方法的末尾,将执行以下语句,并且之前不会进行任何空检查:

xr.date2 = ConvertToDateString(_record.modifiedDate);

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

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

希望有帮助。

关于c# - 为什么我不能将此对象作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16406303/

相关文章:

c# - 删除 Grid 的指定子元素

c# - 将 short 转换为 bool Entity Framework

c# - 在 Kinect v2 中强制关闭红外发射器

android - wordpress - 如何将所有内容和页面的 Web 服务与 Android 应用程序集成

SAP 项目的 Web 服务粒度?

python - 如何通过函数按 ASCII 降序对单个字符串输出进行排序?

c - 函数根据数字删除单词 - C 编程

c# - 如何在 C# 中动态命名变量?

java - 来自 Java Web 服务的空响应对象

c# - methods() 应该放在哪里?