2

I'm building an ASP.NET web service.

I've got my code defined as below, but I can't figure out how to the the wsdl to specify the minOccurs of the FirstName and LastName properties. I want those as required, and can not be empty. Is it possible?

[WebMethod()]
public void TestMethod(TestClass Test)
{
    ...
}

[Serializable]
public class TestClass
{
    public string FirstName { get; set; }
    public string LastName { get; set; }    
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • in case you stumble here hoping for the opposite (`minOccurs=0`) see http://social.msdn.microsoft.com/Forums/en-US/30d3517b-98c5-44d0-b621-4f3343ce8ea2/getting-wsdl-to-produce-minoccurs0 -- you add an ignored property `[XmlIgnore]public bool {{TheProperty}}Specified { get; set; }` and set it when 'TheProperty' is set. – drzaus Nov 05 '13 at 17:42

3 Answers3

3

It turns out that the WSDL is not used to validate incoming XML. It wouldn't matter whether or not you could specify minOccurs - it would not be used to validate the input.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    Is this empirical knowledge or have you found any MSDN documentation material stating this fact? I want to learn more about WCF/WSDL/validation and this seems to be very interesting. – D.R. Dec 17 '13 at 09:09
  • It's empirical knowledge, mostly. Also, there are articles on how to use a SOAP Extension to validate. This implies that no validation is performed without the use of the SOAP Extension. Finally, there's common sense. If validation were performed, then there would need to be a way to turn it off. There is no such switch. – John Saunders Dec 17 '13 at 10:53
  • With "SOAP-Extension" you mean writing a MessageInspector like shown at http://msdn.microsoft.com/en-us/library/aa717047.aspx ? – D.R. Dec 17 '13 at 11:29
  • 1
    This question was about legacy ASMX web services, so no, I meant a class deriving from SoapExtension. Yes, for WCF, you would use a MessageInspector. – John Saunders Dec 17 '13 at 11:34
0

Strings are reference types and so by definition nullable. If your property was an integer minoccurs would have been 1.

You can force the Serializer not to allow it to be null, by putting. [XmlElement("name", IsNullable=false)]
above the property.

Edit: I meant reference types instead of value types. Thnx Joren!

Yvo
  • 18,681
  • 11
  • 71
  • 90
  • I'm not sure that's correct. I think IsNullable governs whether xsi:nil is permitted. I don't believe there's a way to influence minOccurs. – John Saunders Jul 20 '09 at 23:42
  • Strings aren't value types, and value types are not nullable (except when they are, as in `int?`). – Joren Nov 20 '09 at 11:04
0

I have posted the detailed answer on another thread with the same problem: How to make a dotnet webservice set minOccurs=“1” on a string value.

However the answer for strings is no.

The only way make minOccurs=1 without nullable=true is to declare a property with no default value (string has a default value of String.Empty) and without a property to check if the value was specified (making an identical property name with "Specified" word appended to it's name).

And you are still limited if John Saunders' answer is true.

It turns out that the WSDL is not used to validate incoming XML. It wouldn't matter whether or not you could specify minOccurs - it would not be used to validate the input.

Community
  • 1
  • 1
Rapps
  • 142
  • 1
  • 6