Tuesday, January 22, 2013

How to prevent 'Specified' properties being generated in WCF clients?

How to prevent 'Specified' properties being generated in WCF clients?

I have two .NET 3.5 WCF services build with VS2008.

I have two WCF clients in Silverlight to consume these services. The clients are generated with the 'Add Service Reference'. I am using Silverlight 4.

ONE of the proxies is generated with Specified properties for each property. This is a 'message-in' class for my service method :

    // properties are generated for each of these fields     private long customerProfileIdField;             private bool customerProfileIdFieldSpecified;             private bool testEnvField;             private bool testEnvFieldSpecified; 

Now my other service (still with a Silverlight client) does NOT generate Specified properties.

Now I don't care about 'tenets of good SOA'. I just want to get rid of these damn properties because in the context of what I'm doing I absolutely hate them.

There has to be some difference between the two services - but I don't want to have to completely rip them apart to find out the difference.

A similar question before had the answer 'you cant do it' - which is definitely not true because I have it - I just don't know what I did differently.

Edit: I am now in a situation where I regenerate my Silverlight 4 proxy to my 3.5 WCF service (all on the same localhost machine) that sometimes I get 'Specified' properties and sometimes I don't. I no longer think (as I suspected originally) that this is due solely to some endpoint configuration or service level [attribute]. Theres certain triggers in the message itself that cause Specified to be generated (or not). There may be many factors involved or it may be something very simple.

Answers & Comments...

Answer: 1

try this in your WCF service where the property is declared

[DataMember(IsRequired=true)] public bool testEnvField { get; set; } 

IsRequired=true will negate the need for the testEnvFieldSpecified property

by : Neilhttp://stackoverflow.com/users/61480

Answer: 2

OK I've found one thing so far that will cause Specified properties to be generated:

  • The presence of an XTypedElement in the message.

These are used by Linq2XSD. I was returning an element from a Linq2XSD model.

This triggered Specified properties to be generated EVERYTHING in all my classes :

    public XTypedElement Foo { get; set; } 

This however didn't :

    public XElement Foo { get; set; } 

Still curious as to why this is, and if there are any other things that trigger this.

by : Simon_Weaverhttp://stackoverflow.com/users/16940

Answer: 3

These extra Specified properties are generated for value types which are being specified as optional in either the contract or the attribute markup.

As value types have a value by default, the extra Specified flags are being added for these properties, to allow the client (and server) to distinguish between something explicitly not specified or explicitly specified - which may well be set to the default value. Without it, integers would always end up being 0 (and being serialized) even if you don't set them (because of the mapping to int) in your client code. So when you do, you need to also make sure that you set the Specified flag to true, otherwise these properties will not get serialized.

So to prevent these flags being generated for value types, you would have to change the contract to make these value type properties mandatory, instead of optional.

Hope that makes sense.

by : Wim Hollebrandsehttp://stackoverflow.com/users/30874




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog