3

I have a web service with a method that receives and object. One of the properties of the object is a string. The default value is string.empty for this property.

Sometimes I do want to set null on that object and send it. But if I do that and inspect the object is received in debug mode I see that an empty string has been received.

I have tried to call the webservice in two ways, both results in the same string.empty value;

  • by creating an web service call object from a service reference
  • by invoking on a web service proxy

Any ideas how to solve this?

1
  • Good chance that since the default is String.Empty that any NULL value will be replaced with the default. Change default to NULL and see if that makes a difference
    – Quintium
    Apr 29, 2014 at 13:24

2 Answers 2

0

The point of a default value is that it's going to be used when nothing is provided. There's no way for the system to know whether it's received a null value because you explicitly provided it a null value or because you didn't provide a value, so if you tell it to default to String.empty then it's going to do so when it gets a null value. You're going to have to rethink your use of a default if you do want to be able to pass it null values and have them received as such.

However, here's a thought: if you reverse your usages of string.Empty and null, the default behavior is to convert string.Empty values to null, but there's a data annotation that'll prevent this:

[DisplayFormat(ConvertEmptyStringToNull = false)]

So maybe you can use null as your default, pass it string.Empty in the cases where you would now be passing it null, and tell it to let those go through by using that data annotation?

0

I believe using a custom serializer will achieve this. By default null strings are passed as empty over the service boundry iirc.

Look at ixmlserialisable or iserializable

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable(v=vs.110).aspx

also if using wcf

http://msdn.microsoft.com/en-us/library/ms731073(v=vs.110).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.