当两个新值添加到SOAP Web服务响应时,.Net突然将响应中的空数组视为null,而不是空。为什么是这样?

时间:2022-08-04 15:30:53

Background:

We have a SOAP web service in production, and a new instance of it in test. The new version adds two new values to the response, but changes nothing else. I used SOAP UI to inspect the actual responses, and here they are. Namespaces changed for privacy.

我们在生产中有一个SOAP Web服务,并在测试中有一个新的实例。新版本为响应添加了两个新值,但不会更改任何其他值。我使用SOAP UI来检查实际的响应,这里是它们。名称空间已更改为隐私。

TEST:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <OutputParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://some.com/method/">
         <X_CONFIG_INFO>
            <SOVALID>1</SOVALID>
            <LINEVALID>0</LINEVALID>
            <BUILDVALID>1</BUILDVALID>
            <STATUS_MESSAGE>Invalid Sales Order Line Number for Sales Order: 10 and Line Number: 1</STATUS_MESSAGE>
            <SALESORDER>10</SALESORDER>
            <LINENUMBER>1</LINENUMBER>
            <LINEQUANTITY xsi:nil="true"/>
            <PARTNUMBER xsi:nil="true"/>
            <CUSTNUM xsi:nil="true"/>
            <CUSTNAME xsi:nil="true"/>
            <BOMSW/>
            <BUILDWITH/>
            <OPTIONS/>
            <ATTACHMENT/>
            <SMEN>N</SMEN>
         </X_CONFIG_INFO>
      </OutputParameters>
   </env:Body>
</env:Envelope>

PRODUCTION:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <OutputParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://some.com/method/">
         <X_CONFIG_INFO>
            <SOVALID>1</SOVALID>
            <LINEVALID>0</LINEVALID>
            <SALESORDER>10</SALESORDER>
            <LINENUMBER>1</LINENUMBER>
            <LINEQUANTITY xsi:nil="true"/>
            <PARTNUMBER xsi:nil="true"/>
            <CUSTNUM xsi:nil="true"/>
            <CUSTNAME xsi:nil="true"/>
            <BOMSW/>
            <BUILDWITH/>
            <OPTIONS/>
            <ATTACHMENT/>
            <SMEN>N</SMEN>
         </X_CONFIG_INFO>
      </OutputParameters>company
   </env:Body>
</env:Envelope>

The four properties: BOMSW, BUILDWITH, OPTIONS, ATTACHMENT are arrays. When there is no data, they come across as empty elements.

四个属性:BOMSW,BUILDWITH,OPTIONS,ATTACHMENT是数组。当没有数据时,它们就会成为空元素。

I used Beyond Compare to compare the two responses, and they are identical except that the Test version has two new elements in the response: BUILDVALID and STATUS_MESSAGE.

我使用Beyond Compare来比较两个响应,它们是相同的,除了Test版本在响应中有两个新元素:BUILDVALID和STATUS_MESSAGE。

I have a .Net app that has a Service Reference (WCF) to this web service. I have two versions of that app. I'll call them "old" and "new". The old code is currently deployed on thousands of machines in our company. Not every machine needs the new version with the new values, but they might occasionally call the new method with the old code. The new client code will only be deployed to a few machines at first, but the web service obviously is all or nothing.

我有一个.Net应用程序,它具有此Web服务的服务引用(WCF)。我有两个版本的应用程序。我会称他们为“老”和“新”。旧代码目前部署在我们公司的数千台机器上。并非每台机器都需要具有新值的新版本,但它们有时可能会使用旧代码调用新方法。新客户端代码最初只会部署到少数几台机器上,但Web服务显然是全部或全部。

I would not have thought that adding a couple of values to the output XML would make a difference to the old code. If the service proxy code generated by Visual Studio uses XPath to read the values, then it should totally ignore the new values.

我不会想到在输出XML中添加几个值会对旧代码产生影响。如果Visual Studio生成的服务代理代码使用XPath来读取值,那么它应该完全忽略新值。

Problem:

In the old code, the .Net service proxy translates the empty elements in the Production response into empty arrays.

在旧代码中,.Net服务代理将生产响应中的空元素转换为空数组。

When the same code receives the Test response above, it translates the empty elements into null arrays.

当相同的代码收到上面的Test响应时,它会将空元素转换为空数组。

This crashes the old code because it doesn't check for nulls. This is obviously a bad practice, but it is what it is. The code is already in production. The new code has been altered to check for nulls, but due to logistic issues, it can't be deployed everywhere at once.

这会使旧代码崩溃,因为它不会检查空值。这显然是一种不好的做法,但事实就是如此。该代码已经投入生产。新代码已被更改为检查空值,但由于后勤问题,它无法一次性部署到任何地方。

Why would adding two new values to the XML response cause WCF to turn the empty arrays into null arrays?

为什么在XML响应中添加两个新值会导致WCF将空数组转换为空数组?

I should add that the webservice is hosted in Oracle. It's based purely of of a pl-sql package that get auto generated into a web service. There's no chance to do anything fancy on the server side.

我应该补充一点,Web服务托管在Oracle中。它完全基于pl-sql包,可以自动生成Web服务。没有机会在服务器端做任何花哨的事情。

1 个解决方案

#1


I am not sure how you wrote the code to add those two properties. I presume DataContracts so I will suggest an answer along those lines.

我不确定你是如何编写代码来添加这两个属性的。我推测DataContracts所以我会建议这样的答案。

The is answer is one word Versioning. Thats it.

答案是一个单词Versioning。而已。

Check the following articles and references for implementation.

请查看以下文章和参考资料以获取实施。

https://msdn.microsoft.com/en-us/library/ff384251.aspx

http://www.codeproject.com/Articles/352586/WCF-Backward-Compatibility-and-Versioning-Strate

Hope it helps.

希望能帮助到你。

#1


I am not sure how you wrote the code to add those two properties. I presume DataContracts so I will suggest an answer along those lines.

我不确定你是如何编写代码来添加这两个属性的。我推测DataContracts所以我会建议这样的答案。

The is answer is one word Versioning. Thats it.

答案是一个单词Versioning。而已。

Check the following articles and references for implementation.

请查看以下文章和参考资料以获取实施。

https://msdn.microsoft.com/en-us/library/ff384251.aspx

http://www.codeproject.com/Articles/352586/WCF-Backward-Compatibility-and-Versioning-Strate

Hope it helps.

希望能帮助到你。