Background:
- WCF service proxy not setting "FieldSpecified" property
- MSDN Forums: parameter-names-ending-with-specified-added-to-web-service-method-call?forum=wcf
I am fed up with having to manually set dozens of "_Specified" fields in my web-service client (for Struct
datatypes like Long and DateTime), so I thought I'd try looping through all of the properties in my Soap Body and if it's a Boolean called Specified AND if the property (such as a Birthdate) has been set, then I could default that property to true.
So I wrote this code to loop through, and check if the property isn't null then if not, set the corresponding Specified property to true:
Type type = wsSoapBody.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
foreach (var pi in wsSoapBody.GetType().GetProperties().Where(o => o.Name == property.Name + "Specified"))
{
if (property != null)
{
pi.SetValue(wsSoapBody, true, BindingFlags.SetField | BindingFlags.SetProperty, null, null, null);
}
}
}
Unfortunately, even if some properties haven't been set, property != null
is always true, so even when a field has not been set, this Boolean value is being set to true, and that's not what I want.
How could I do this properly?
EDIT:
to clarify, say I have a field in my soap body type called Birthdate
(which is a datetime) then in WCF web-service client proxy there will also be a property called BirthDateSpecified
and that is what I need to set to true if BirthDate
has been set.