Can WCF contract methods return null?

I have a fixed size multidimensional set exposed through a WCF contract and I want to be able to return null for any coordinates in a collection that hasn't been populated. When I try to do this, I get an exception indicating that this is not supported: "FaultException`1: An object reference is not set on an object instance."

I wondered if any OperationContract flag could be used, but none is highlighted.

Is this what I want, or is there some internal limitation in WCF?

thank

+2


source to share


3 answers


In WCF, nothing prevents you from returning null from any method of a contract operation.



The exception is FaultException<NullReferenceException>

, which means that somewhere in your server-side code you are referring to an object that is set to null. Check your server-side code.

+5


source


Yes, they can, I do it all the time. Could you please post the relevant bits of the service contract and / or data contracts so we can see where the problem might be?



+3


source


As both Garth and Christian have argued, you can indeed return null

from WCF contract methods. In fact, I was caught by my (attempt) cleverness, where the outer (contract) type was converted from the inner type using an implicit conversion operator, within the framework of which the properties of the inner type were available as arguments to the constructor of the outer type. Consequently, NullReferenceException

.

Doh! to me. Quest for G and C. Gart gets to "take" hair, since reading it gave me an instant light bulb.

+1


source







All Articles