2

I am using a WCF webservice in my application, while i am sending a request im monitoring the request and responses with fiddler. the problem is for null tags, wcf client generates nil=true attribute. is there anyway i could change the them to blank tags ? here is the example

<a:Seats i:nil="true" />

which is list of Seat class, it is optional tag so it is not necessary to pass it. but when i send a request like above i receive Object reference not set to an instance of an object response from the web service. i have been asked to remove the nill attribute from the request. so it must become like this

<a:Seats/>

Adding EmitDefaultValue=false will remove the tag completely so it is not the answer. how can i do that ?

Behzad
  • 857
  • 1
  • 8
  • 27
  • what do you mean with "blank tags"? – Jocke Mar 18 '15 at 09:25
  • 3
    ***Why*** do you need this?? The `i:nil=true` just simply states that there is no value for this property.... why is that a "problem" ?? – marc_s Mar 18 '15 at 09:35
  • @Jocke blank like , – Behzad Mar 18 '15 at 09:42
  • @marc_s i have no idea why i:nill=true can make a problem. even if in server side its not specified ,it should be ignored but since they advice me to remove it i need to do so. – Behzad Mar 18 '15 at 09:44
  • @Behzad who is *they*? and why do *they* believe that removing `i:nil="true"` will solve the problem?! anyway, why not initialize the collection with zero elements instead of sending a null-collection (which would be best practice, see: http://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection) –  Mar 18 '15 at 12:00

3 Answers3

0

If I understand your scenario correctly, you want to represent a “list of Seat class” (e.g. List<Seat>) as "empty" rather than null.

The WCF serializer will only allow an object to be serialized to a “blank” value if that object has a “valid” blank value (e.g. a string can be blank or null). xsi:nil indicates that an element is valid with no content, even if that underlying object’s data type does not allow empty elements.

In the event that you want to force your WCF client to send empty elements, you could implement a custome message inspector to modify the message prior to send.

Refer to the How to inspect and modify WCF message via custom MessageInspector for additional information.

Seymour
  • 7,043
  • 12
  • 44
  • 51
0

I figure it out myself,

I Re-Configure the ServiceReference and changed the collection type to System.Collection.Generic.List (at first it was System.Array)

then when i want to make the request simply initialize the request object like below

rq.Seats = new List<Seat>();   // result <a:Seats/>

I know its strange but when it was array the result was different

rq.Seats = new seat[] { } ;    //result <a:Seats nil="true" />
Behzad
  • 857
  • 1
  • 8
  • 27
0

I figure it out by myself.

To avoid this issue in case of NULL xml element in WCF request

Use attribute above the XML node

 [XmlElement(IsNullable = true)]
Gareth
  • 5,140
  • 5
  • 42
  • 73