Webservice with optional parameters

  • Thread starter Thread starter Sam Shrefler
  • Start date Start date
S

Sam Shrefler

I'm working on creating a WebService / WebMethod to receive a record
in real time from another system. The record contains about 20
fields. 10 of which aren't required.

I was planning on just making a method with 20 parameters. But, I see
there is no way to make an "optional" c# parameter. I was just
wondering if anyone had an suggestions on how to implement this? The
parameters are all different primitive types (int, string,
datetime)....

thanks

Sam
 
I'm working on creating a WebService / WebMethod to receive a record
in real time from another system. The record contains about 20
fields. 10 of which aren't required.

I was planning on just making a method with 20 parameters. But, I see
there is no way to make an "optional" c# parameter. I was just
wondering if anyone had an suggestions on how to implement this? The
parameters are all different primitive types (int, string,
datetime)....

Make it take a single parameter of a type which encapsulates the
record. For optional reference type fields, use null to avoid
specifying the value. For optional value type fields, either have a
separate flag or have a "magic" value which means "no real
value" (e.g. int.MinValue for integers).

Jon
 
Is there any way to use Nullable<int> or Nullable<dateTime> in the in
the record?

Thanks

Sam
 
I'm sorry,I guess I didn't explain myself very well there.

I have a webMethod called saveUser

It must receive user information. Some of the information needs to be
optional. Some is required (represented with a *):

string firstName*
string middleName
string lastName*
dateTime birthday*
dateTime anniversary
dateTime death
int userTypeID

I need my method able to accept all of them, yet, it can't force them
to send an anniversaryDate time...

Any suggestions on something like that?

Thanks

Sam
 
Well, if that's the way you must do it, then why not just leave the optional
parameters "blank" - either an empty string ( "") or null, whichever fits
your types, when the values are passed into your method?

For a DateTime, you can pass DateTime.MinValue (which is not a "null") and
just check for that.

So, you can check to see if optional parameters have a value, and decide
what to do.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
 
I'd rather not use minValue since this webservice will be consumed by
many different platforms. Instead what i am trying to do is declare
my params as DateTime? and int?

That way, hopefully the user can just send an empty param <myDateTime/
and that will be received as null.

I'm creating and waiting for some tests to occur.

Does this seem like a viable solution?
 
Sam Shrefler said:
I'd rather not use minValue since this webservice will be consumed by
many different platforms. Instead what i am trying to do is declare
my params as DateTime? and int?

Using int.MinValue is a *lot* more portable than using nullable types.
How would you expect Record to be represented on a platform which
doesn't have nullable types?

Compare that with using int.MinValue, which will always be the minimum
value of a 32-bit 2's complement integer.

Using DateTime.MinValue is a bit riskier - you'd have to have a well-
defined date/time which is representable on all target platforms, and
use that everywhere.
 
Back
Top