Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebMethod arguments validation

Status
Not open for further replies.

123qweasdzc

Programmer
Sep 27, 2005
20
0
0
PT
I have a simple "helloWorld" webService:

using System;
using System.Web;
using System.Web.Services;

namespace WebServiceTest
{
public class WebServiceTest : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(byte number)
{
return "Hello World" + number;
}
}
}

How and where can I validate if the "number" is actually a byte type? If I write the number 256 the exception is raise:

System.ArgumentException: Cannot convert 256 to System.Byte.
Parameter name: type ---> System.OverflowException: Value was either too large or too small for an unsigned byte.
at System.Byte.Parse(String s, NumberStyles style, IFormatProvider provider)
at System.String.System.IConvertible.ToByte(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
 
You could try using Byte.TryParse()


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Dear Ca8msm,

you are saying that I should try to parse the number argument inside the HelloWorld method? Unfortunately I cannot do that because the exception is raised before. Writing a string or a number > 255 raises an exception before entering the HelloWorld method.
And that’s way I am asking about where and how this kind of validation should be made.

Thanks
 
What you could do is create a public property that sets a private variable which in turn is used by the function. Inside the public property, check what the value has been set as (using Byte.TryParse) and if it is valid set the private variable.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Doesn't seem like this has to do with the web service, but validation on the front end. If I have a function that expects an Integer, but I pass it a String then it will blow up on me. You need to catch it before making the call to the web service (or ca8msm's way would probably work too).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top