Ronnie
Ronnie

Reputation: 670

Determine if Optional Parameter was passed in WCF Soap Service

I am trying to determine if optional parameters were passed to my WCF service. For example assume I have a simple input object defined as follows.

[DataContract]
public class TestObject
{
    [DataMember(IsRequired=false)]
    public int OptionalIntegerField { get; set; }
    [DataMember(IsRequired = false)]
    public bool OptionalBooleanField { get; set; }
    [DataMember(IsRequired = false)]
    public string OptionalStringField { get; set; }
}

And a service contract defined as

[OperationContract(Name = "TestMethod")]
void TestMethod(TestObject obj);

If I have TestMethod defined as

    public void TestMethod(TestObject obj)
    {
        Debug.WriteLine(obj.OptionalBooleanField);
        Debug.WriteLine(obj.OptionalIntegerField);
        Debug.WriteLine(obj.OptionalStringField);
    }

My problem is that if I make a SOAP call to TestMethod with no input parameters specified, OptionalIntegerField and OptionalBooleanField (being Value types) are set to their default values (0 and false). I need to determine whether or not an optional Parameter was passed in.

My question is what is the appropriate way to deal with this? I was thinking about making my OptionalIntegerField and OptionalBooleanField nullable types, but I am not sure if that is the right approach.

Upvotes: 0

Views: 1523

Answers (1)

Chris Hannon
Chris Hannon

Reputation: 4134

Assuming that your reference types are also using the concept of null in the same way, where null means that a value is not present, nullable value types are a perfectly acceptable way of indicating that possibility for value type members.

Upvotes: 2

Related Questions