Reputation: 924
I am coding a client for a web service. In the web service definition file, "minOccurs" and default values are defined. How can I access these minOccurs and defaults?
In the WSDL file, the element is defined like this:
<xs:element minOccurs="0" name="optionalEntry" type="tns:optionalValueType" default="NULL"/>
where optionalValueType
is an enumeration.
The web service is referenced as webservice
and the value NULL
can be set manually (like any other possible value) as webservice.optionalValueType.NULL
.
Upvotes: 4
Views: 1685
Reputation: 390
Oversimplifying, WSDL describes an XML schema for documents which can be consumed by a web service endpoint. The default
attribute is part of XML Schema standard, and the standard tells that value specified in this attribute should be automatically assigned to the element when no other value is specified (https://www.w3schools.com/xml/schema_simple.asp).
There are some tools which can generate client side code from a given WSDL document. You didn't specify which tool you used, so I assume it is WSDL.exe (part of Visual Studio), but there are others (e.g. SoapUI). So, answer to your question depends on the tool you use.
Strictly speaking, the tools are not obliged to provide an API to expose value of default
attribute. They should only generate code which will behave like defined in the standard, namely, if value is not specified, default
value should be used. Example:
// Account property is defined like this:
// <s:element minOccurs="0" maxOccurs="1" name="account" type="s:string" default="FOO" />
var connInfo = new ConnectionInfo();
Console.WriteLine(connInfo.account); // Will print "FOO".
Going back to your question, I can see the following ways to get default
value from the generated client code:
default
values (like in the code snippet above).DefaultValueAttribute
to every property which has default
value. Example (generated code):public partial class ConnectionInfo
{
private string accountField;
public ConnectionInfo()
{
this.accountField = "FOO";
}
[System.ComponentModel.DefaultValueAttribute("FOO")]
public string account
{
get
{
return this.accountField;
}
set
{
this.accountField = value;
}
}
}
So it should be possible to get the value from that attribute, using some reflection:
var type = typeof(ConnectionInfo);
var prop = type.GetProperty("account");
var attr = (DefaultValueAttribute)prop.GetCustomAttributes(
typeof(DefaultValueAttribute), true).First();
Console.WriteLine(attr.Value); // Will print "FOO".
As of minOccurs
attribute, I cannot see a way to get it, other than reading the WSDL schema yourself.
Upvotes: 4