Groups | Blog | Home
all groups > dotnet web services > september 2004 >

dotnet web services : 3 newbie/C#/WebServices questions


William Campbell
9/27/2004 6:29:48 PM
I've never used C# nor have I created Web Services, so I am in the process
of finding tutorials on the web and going through them. I found this one
over at code project:

http://www.thecodeproject.com/cs/webservices/myservice.asp

I am presented with this method:

[WebMethod]
public ClientData[] GetClientData(int Number)
{
ClientData [] Clients = null;

if (Number > 0 && Number <= 10)
{
Clients = new ClientData[Number];
for (int i = 0; i < Number; i++)
{
Clients[i].Name = "Client " + i.ToString();
Clients[i].ID = i;
}
}
return Clients;
}

Anyhow, on the web page for GetClientData, I decide not to type in a value
for the Number field and I click the 'Invoke' button. I receive this:

System.ArgumentException: Cannot convert to System.Int32.
Parameter name: type ---> System.FormatException: Input string was not in a
correct format.
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo
info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType,
IFormatProvider provider)
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(NameValueC
ollection collection)
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest
request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Question #1:
What can I use to determine if a user has passed a parameter correctly? Or
even at all? Is the value null - since I didn't put in a value? But how wo
uld you compare an int to null!?

Question #2:
Is it possible to have default parameters (so it would default to a certain
value)?

Question #3:
How do I DEBUG a Web Service? I set a breakpoint so I could try to figure
out what the Number value was - I pressed F5, it launched a browser, I
clicked the Invoke button - it didn't reach my breakpoint.

Yep. As you can see, I am really brand-y-newbie here.

Thanks!

Sami Vaaraniemi
9/28/2004 10:50:01 PM
Comments inline:

[quoted text, click to view]

Yes, if you do not provide a value for the int parameter in the web page,
then the call fails already before your code in the web method gets a chance
to execute. If you want to allow passing nulls then one way to do this is to
wrap the int into a simple class and use it as the parameter to the web
method

public class NullableInt
{
public int Value;
}

[WebMethod()]
public void ThisMethodAllowsNullArgument(NullableInt nullableInt)
{
if (nullableInt == null) ...
}

Note that if you use this class then you can't invoke the web method through
the browser any more because the browser form only works with primitive
types.

[quoted text, click to view]

ASP.NET Web Services have no support for default values out of the box. For
more flexibility, you could pass e.g., an XML document as a parameter
instead of an int, and implement any default value logic in application
code.

[quoted text, click to view]

Debugging works as you would expect except in this case the exception occurs
before execution reaches your breakpoint.

Regards,
Sami


AddThis Social Bookmark Button