views:

478

answers:

2

I' 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; }    
}
A: 

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!

Zyphrax
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
Strings aren't value types, and value types are not nullable (except when they are, as in `int?`).
Joren
A: 

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