簡體   English   中英

C# 客戶端 Java Web 服務 - 數據互操作性問題

[英]C# client for Java Web Service - data interoperability issue

我有 Java Web 服務,並且一個 web 方法的參數是自定義 ZD523877880E1EA212871 類型

public class KeyList {
public Integer key;
public Integer[] nums ;
public Integer result;
}

Web服務更新Result值並將KeyList object返回給客戶端。

我有一個 C# 客戶端到此 Web 服務(在 Visual Studio 中通過添加服務引用並指向 wsdl Z5727D14E4281E8A02 生成) When I receive the keyList object in C#, the first part ( Integer key ) comes out as 0. If I change the Java custom type to use int key ( rather than Integer Key ) in the KeyList type, then it works fine for the C#客戶。

我想看看 wsdl 在兩種情況下是否有很大不同(使用 int 和 Integer ),但事實證明唯一的區別是 minOccurs 屬性。

使用 Integer 鍵時

<xs:element name="key" type="xs:int" minOccurs="0" /> 

使用 int 鍵時

<xs:element name="key" type="xs:int" /> 

C# 客戶端無法正確接收服務返回值中更新的 Integer 的原因是什么? 不用說,無論哪種方式,它都適用於 Java 客戶端。


編輯:VS為KeyList生成的C# class:

公共 class 密鑰列表:INotifyPropertyChanged{

private int keyField;
private bool keyFieldSpecified;

private int?[] numsField;
private PropertyChangedEventHandler PropertyChanged;
private int resultField;
private bool resultFieldSpecified;

public event PropertyChangedEventHandler PropertyChanged;

public keyList();
protected void RaisePropertyChanged(string propertyName);

[XmlElement(Form=XmlSchemaForm.Unqualified, Order=0)]
public int key { get; set; }
[XmlElement("nums", Form=XmlSchemaForm.Unqualified, IsNullable=true, Order=1)]
public int?[] nums { get; set; }
[XmlElement(Form=XmlSchemaForm.Unqualified, Order=2)]
public int result { get; set; }
[XmlIgnore]
public bool resultSpecified { get; set; }

}

java中的 Integer 可以是 null 這就是你看到minOccurs="0"的原因。 int不能是 null,這就是缺少 minOccurs 的原因。 在 C# int (或相同的 Int32 )中不能是 null 所以它不期望minOccurs="0" 問題很可能出現在 Visual Studio 代理生成器中。

它應該在 C# ( Int32?int?Nullable<Int32> )中為此元素生成可為的 int :

<xs:element name="key" type="xs:int" minOccurs="0" /> 

根據您生成此代理的方式,Visual Studio 可能添加了名為“keySpecified”的額外布爾字段。 它有時會在可為空字段的名稱中添加“指定”后綴。 它希望您編寫如下代碼:

if(res.keySpecified){
    // use res.Key
} else {
    // assume res.Key is null
}

如果尚未生成“keySpecified”,您可以查看使用svcutil生成代理。 它比 Visual Studio 中提供的選項多得多。 另一個可能的解決方案是在 java 端堅持 int ,因為它直接映射到 C# 端的 int 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM