Problems making a string property mandatory in Web Services

Source Link

I am developing a web service for a customer. The service has a method, that
returns an array of Forms. Each form has a Name property of Type String.

The customer wants the contract to specify, thath the name is mandatory and
not nullable. To reflect this, they want the xml in the wsdl (the Form part)
to look similar to this:

<xs:complexType name="Form">
<xs:sequence>
...
<xs:element minoccurs="1" maxoccurs="1" name="Name" nillable="false" type="xs:string">
...

So, minOccurs should be 1 and nillable should be false (or omitted), to
ensure the value is present and not null.

No matter how I go about this, however, I cannot make the wsdl generate the
desired values. I have tried to do contract-first development using WSCF from
thinktecture, and have the schema dictate the above values for minOccurs and
nillable, but this approach has not remedied the problem.

Is it at all possible to do what I want using .Net and Visual Studio 2005? I
hope my problem is described adequately, otherwise please request more
information. Any help is greatly appreciated.


Regarding on the webservice class property definition problem you
mentioned, here are some of my understanding and suggestion:

1. .NET webservice will always generate the xsd scheme for class property
as below:

** for value type(primitive types or struct), it will use minOccurs="1"
since value type will always be assigned a value(doesn't have null value
unless you use nullable type).

** for reference type(such as normal class), it will use minOccurs="0"
since value type will always support null value, the "string" type in your
case is just conform to this policy.

So far this rule is not changable in our custom code(attribute).

2. Also, XML XSD schema definition's element definition doesn't 100%
completely identical to .NET(or OO ) class/type definition. For example, if
you have the following xsd schema

<element minOccurs="1" ..../>

you can not find a reference type to mapping it since reference type always
support null reference value. I think you can consider define a wrapper
class(which inherit from ValueType ), property of such type will be
automatically generated as minOccurs="1" in XSD schema. However, you still
need to ensure that any sub property of reference type(such as string) has
been supplied a value in your own code.

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead