C# client for Java Web Service - data interoperability issue
I have Java Web service , and a parameter to one web method is a custom Java type
public class KeyList {
public Integer key;
public Integer[] nums ;
public Integer result;
}
The Web service updates the Result value and returns the KeyList object to the client.
I have a C# client to this Web service ( generated in Visual Studio by adding Service Reference and pointing to the wsdl url ). 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# client.
I wanted to see if the wsdl was drastically different between the two cases ( using int and Integer ) , but it turns out that the only difference is a minOccurs attribute.
when using Integer key
<xs:element name="key" type="xs:int" minOccurs="0" />
when using int key
<xs:element name="key" type="xs:int" />
What is the cause for the C# client not correctly receiving the updated Integer in the return value from the service ? Needless to say it works fine for a Java client either way.
Edit : C# class generated by VS for KeyList :
public class keyList : INot开发者_运维百科ifyPropertyChanged{
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; }
}
Integer in java can be null that is why you see minOccurs="0"
. int can not be null and this is why minOccurs is missing. In C# int (or Int32 which is the same) can not be null so it does not expect minOccurs="0"
. The problem is most likely in Visual Studio proxy generator.
It should have generated nullable int in C# (Int32?
or int?
or Nullable<Int32>
) for this element:
<xs:element name="key" type="xs:int" minOccurs="0" />
Depending on how you generated this proxy, Visual Studio might have added additional bool field called 'keySpecified'. It sometimes adds "Specified" suffix to the name of nullable field. It expects you to write code like this:
if(res.keySpecified){
// use res.Key
} else {
// assume res.Key is null
}
If 'keySpecified' has not been generated you can look at generating proxy with svcutil. It has a lot more options than what is exposed in Visual Studio. Another possible solution for you is to just stick with int on java side because it maps directly to int on C# side.
精彩评论