Monday, March 26, 2012

How do I create a WebService with mandatory elements in the request?

I need a bit of assitance creating a WebService. Say that I have something like this -

'The objectsPublic Class productPublic nameAs StringPublic priceAs DecimalEnd ClassPublic Class orderPublic customerAs StringPublic productsAs List(Of product)End Class'The DB layerPublic orderDBPublic Function commitOrder(orderAs order)As IntegerDim orderIdAs Integer'DB code ommittedReturn orderIdEnd FunctionEnd Class'The Web Service<System.Web.Services.WebService(Namespace:="http://localhost/")> _<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ToolboxItem(False)> _Public Class order_wsInherits System.Web.Services.WebService <WebMethod()> _Public Function commitOrder(ByVal orderAs order)As Integer Dim orderDBAs New orderDB()Return orderDB.commitOrder(order)End FunctionEnd Class
The WSDL I get contains something like -

<s:element name="commitOrder"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="order" type="tns:order" /></s:sequence></s:complexType></s:element><s:complexType name="order"><s:sequence><s:element minOccurs="0" maxOccurs="1" name="customer" type="s:string" /><s:element minOccurs="0" maxOccurs="1" name="products" type="tns:ArrayOfproducts" /></s:sequence></s:complexType><s:complexType name="ArrayOfproducts"><s:sequence><s:element minOccurs="0" maxOccurs="unbounded" name="product" nillable="true" type="tns:product" /></s:sequence></s:complexType><s:complexType name="product"><s:sequence><s:element minOccurs="0" maxOccurs="1" name="name" type="s:string" /><s:element minOccurs="0" maxOccurs="1" name="price" type="s:decimal" /></s:sequence></s:complexType>

Notice that everything is minoccurs="0" and the product entry in the ArrayOfproducts is nillable. How do I make it so that specific bits are required (minoccurs="1" and/or product element not nillable)?

Thanks

Martin

You could set a web service header so they always send you those requiered fields.


I'm not sure that I understand what you mean. But, just to elaborate on what I am looking for, the WSDL should contain tags that specify minOccurs="1".


Might be better to use discrete parameters rather than sending an order object. Most people find that more straightforward to use.


How would you control mandatory / optional elements using discrete parameters?

In any case, I'm already severely restricted in my attempts to use best coding practices by the inadequacies of .Net's WebService implementation. If I can't even control mandatory elements in this scenario I'll give it a miss and look at using Java and Mule.

No comments:

Post a Comment