Missing Parameters in call to WebService
-
I have a trivial webservice set up that takes a single parameter and returns a number of items according to the value of the parameter:
[WebMethod( Description="Returns the top 'count' latest articles.")] public XmlDataDocument GetLatestArticles( int count ) { if (count < 1) count = 1; if (count > 100) count = 100; return ArticleCache.GetLatest( count ); }
I have some simple parameter checking, such that the method will always return at least one article and at most, 100 articles. However, if the client does not provide thecount
parameter at all, aSystem.ArgumentException
is thrown before the webmethod is called and an ugly error is returned to the client. I would like to be able to catch this exception and supply a 'default' value to the inner method call, so that the client continues to function. I tried making thecount
parameter nullable, but then the default WsdlHelpGenerator.aspx doesn't display it, because "The test form is only available for methods with primitive types as parameters." This makes it rather difficult to test - especially as the services are for consumption by a third party developer. Is there any (elegant and easy) way of catching the exception and handling it gracefully? Thanks.Sunrise Wallpaper Project | The StartPage Randomizer | The Windows Cheerleader
-
I have a trivial webservice set up that takes a single parameter and returns a number of items according to the value of the parameter:
[WebMethod( Description="Returns the top 'count' latest articles.")] public XmlDataDocument GetLatestArticles( int count ) { if (count < 1) count = 1; if (count > 100) count = 100; return ArticleCache.GetLatest( count ); }
I have some simple parameter checking, such that the method will always return at least one article and at most, 100 articles. However, if the client does not provide thecount
parameter at all, aSystem.ArgumentException
is thrown before the webmethod is called and an ugly error is returned to the client. I would like to be able to catch this exception and supply a 'default' value to the inner method call, so that the client continues to function. I tried making thecount
parameter nullable, but then the default WsdlHelpGenerator.aspx doesn't display it, because "The test form is only available for methods with primitive types as parameters." This makes it rather difficult to test - especially as the services are for consumption by a third party developer. Is there any (elegant and easy) way of catching the exception and handling it gracefully? Thanks.Sunrise Wallpaper Project | The StartPage Randomizer | The Windows Cheerleader
depending on how you have things set up..i wrote an xml file and from there had a wsdl and server stubs generated, but in the XML i'm pretty sure that you can make the parameter optional, then look for it in the server and give it a default value if it s not there. or you might be able to set a default value in the XML...since you typically need a wsdl and the xml file(maybe) to generate a client app, it should work for everyone. i believe setting the minoccurs attribute to 0 in the xml file makes it optional, but i have not tried it.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.