Optional parameters in ASP.NET web service

22,583

Solution 1

I am assuming that when you say ASP.net web services, you are creating web services with ASMX extension. I think that what happens in this case is that all nullable types become optional and non-nullable become non-optional.

You could perhaps manually edit the generated WSDL file. But then you would have to redo that work if the wsdl was regenerated.

I would suggest that you switch to WCF with basisHttpBinding (except for the name of you service your clients should not notice the difference).

Using WCF you can simply mark the parameter in the data contract as required or not:

[DataMember(IsRequired="false")]

Solution 2

The primitives are not reference types, but rather they are value types. You can make a value type "nullable" a couple ways.

The short-hand is

int? i;

or long-hand here

Nullable<int> i;
Share:
22,583
Thomas Lötzer
Author by

Thomas Lötzer

Updated on December 13, 2020

Comments

  • Thomas Lötzer
    Thomas Lötzer over 3 years

    I have a ASP.NET web service. This web service works fine. However, the WSDL lists some parameters as optional (minoccurs = 0) and others as non-optional. Some of the optional parameters are actually not optional, others which are marked as non-optional are actually optional. I would like to fix this, but I can't find the location where this information is coming from.

    It seems to me that all primitive types (int, boolean etc.) are non-optional and all other parameters are marked as optional. However, I can't find a location where I can change this. I would like to specify default values for the primitive values if they are missing in the request and specify which non-primitive parameter is actually optional. Where do I do this?

  • Bora
    Bora over 11 years
    This answer is almost completely false. You cannot use nullables instead of optional parameters. Since .Net 4.0 you can use optional parameters in any method.
  • Shiraz Bhaiji
    Shiraz Bhaiji over 11 years
    @Bora, the answer is from 2009, at that time this was what was available.
  • Bora
    Bora over 11 years
    Nothing wrong with that. Just update your answer and everything will be well again.
  • Shiv
    Shiv over 8 years
    How do you distinguish between omitting the parameter (and use a default) vs wanting to set the value to NULL?
  • oglester
    oglester about 3 years
    I suppose if you want to distinguish this, you might want to migrate to the Functional pattern of Option/Maybe and not allow nulls. In that paradigm, you would have an explicit object type for Null/Empty. How to get ASMX's to change minoccurs in WSDL, I'm just not sure. You might read more here: stackoverflow.com/questions/3961112/…
  • oglester
    oglester about 3 years
    Also, if you really want your web services to have a pristine XSD/WSDL contract, you can build your web services Contract-first: docs.microsoft.com/en-us/dotnet/framework/wcf/… stackoverflow.com/questions/23000510/…