468,133 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

How to specifiy default values in WCF web services (repost)

Hello,

I am trying to specify a logical default value for a [DataMember] in a WCF
Web Service using basicHttpBinding.

I realize that the language defaults are:

int - 0
string - null
bool - false
enum - ?

I tried using OnDeserializing and OnDeserialized events, but when the client
doesn't explicitely set the value for an int member, for instance, I keep
getting 0. For a bool memeber I get a 'false', and for an enum member it
picks some default, but I don't know how it picks it. The only default that
I can logically work with is the null for reference types, including
strings.

The XSD allows you to specify "default=" attribute for a member, but in WCF
I don't have control over wsdl/schema generation. So I thought there would
be a way to specify a logical default value for a DataMember through an
Attribute settings of some sort, but I can't find one.

Please help!!!

thanks, Dave
Jul 15 '08 #1
4 16246
Hi Dave,

From your description, you're wondering how to set some default value for
some primitive types (int , bool, enum ..) contained in WCF data contract
class, correct?

Yes, .NET Framework has some default values for primitive types such as
integer, bool, .... For WCF DataContract class, the "DataMemberAttribute"
hs some property for control the serlization behavior of default value of
field:
#Data Member Default Values
http://msdn.microsoft.com/en-us/library/aa347792.aspx
Also, WCF data contract class is the same as other normal class, if you
have some class fields which want to have some default value, you can put
the default value in the field's declaration( or in the class's default
constructor). e.g.

==================
[DataContract]
public class MyTestDC
{
private int _count = -1;
private bool _valid;

[DataMember]
public bool Valid
{
get { return _valid; }
set { _valid = value; }
}

[DataMember]
public int Count
{
get { return _count; }
set { _count = value; }
}

public MyTestDC()
{
_valid = false;
}

}
=========================

http://blogs.parivedasolutions.com/j...09/11/523.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Dave Burns" <db****@newsgroup.nospam>
Subject: How to specifiy default values in WCF web services (repost)
Date: Tue, 15 Jul 2008 10:12:58 -0500

Hello,

I am trying to specify a logical default value for a [DataMember] in a WCF
Web Service using basicHttpBinding.

I realize that the language defaults are:

int - 0
string - null
bool - false
enum - ?

I tried using OnDeserializing and OnDeserialized events, but when the client
doesn't explicitely set the value for an int member, for instance, I keep
getting 0. For a bool memeber I get a 'false', and for an enum member it
picks some default, but I don't know how it picks it. The only default that
I can logically work with is the null for reference types, including
strings.

The XSD allows you to specify "default=" attribute for a member, but in WCF
I don't have control over wsdl/schema generation. So I thought there would
be a way to specify a logical default value for a DataMember through an
Attribute settings of some sort, but I can't find one.

Please help!!!

thanks, Dave

Jul 16 '08 #2
Language defaults don't work for me, because they are not logical defaults.

The [EmitDefaultValue=false] only works for microsoft clients using svcutil
generated proxy file, correct? What if it's a Java client consuming my WCF
service? How can I be sure that what came over the wire is a language
default value or my logical default value?

Here's a concrete example:

I have a property called PerfectPrintAngle, it's an integer.
Its logical default value is -1, which tells the web service not to use
PerfectPrintAngle (valid values are from 0 to 359)
Its language default value is 0, which is in the correct range.

So if it comes in over the wire as 0 (language default), the web service has
no way of telling whether this was defaulted on the client side or actually
set to 0 by the client program.

I hope I am explaining this correctly!

Thanks, Dave
Jul 17 '08 #3
Thanks for your reply Dave,

I think I've got the actual issue you mentioned. Actually, you want to find
some means to let the wcf client get informed of the certain default value
for a certain field(no matter what programming platform the client is
using). I'm afraid this is abit difficult since for default value, this
is mostly a programming language layer feature, for example .net framework
class can set default value for class members. The WCF or webservice are
exposing service via WSDL/xml schema , it is difficult to let XML
description to force the client side programming platform generate code
which follow a logical rule. So far I haven't found such a means in xml
schema. For example, you can add some xml annotation/comments to indicate
the client side that what's the expected value/default value for the
certain field, however, it is still depend on the client-side's proxy
implementation (whether it adopt the annotation or WSDL description or
not).

So far I think the most guranteed means is still make sure both sides using
.net framework (actually this is the preferred approach for WCF ) so that
we can enforce such logical rules via code level definition such as default
value (or constructor initialization).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Dave Burns" <db****@newsgroup.nospam>
References: <#x**************@TK2MSFTNGP05.phx.gbl>
<H2**************@TK2MSFTNGHUB02.phx.gbl>
Subject: Re: How to specifiy default values in WCF web services (repost)
Date: Thu, 17 Jul 2008 08:31:21 -0500
Language defaults don't work for me, because they are not logical defaults.

The [EmitDefaultValue=false] only works for microsoft clients using svcutil
generated proxy file, correct? What if it's a Java client consuming my WCF
service? How can I be sure that what came over the wire is a language
default value or my logical default value?

Here's a concrete example:

I have a property called PerfectPrintAngle, it's an integer.
Its logical default value is -1, which tells the web service not to use
PerfectPrintAngle (valid values are from 0 to 359)
Its language default value is 0, which is in the correct range.

So if it comes in over the wire as 0 (language default), the web service
has
no way of telling whether this was defaulted on the client side or actually
set to 0 by the client program.

I hope I am explaining this correctly!

Thanks, Dave

Jul 18 '08 #4
Hi Dave,

Have you got any progress on this or still any further questions or
anything we can help?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

From: st*****@online.microsoft.com (Steven Cheng [MSFT])
Organization: Microsoft
Date: Fri, 18 Jul 2008 06:55:07 GMT
Subject: Re: How to specifiy default values in WCF web services (repost)
Thanks for your reply Dave,

I think I've got the actual issue you mentioned. Actually, you want to find
some means to let the wcf client get informed of the certain default value
for a certain field(no matter what programming platform the client is
using). I'm afraid this is abit difficult since for default value, this
is mostly a programming language layer feature, for example .net framework
class can set default value for class members. The WCF or webservice are
exposing service via WSDL/xml schema , it is difficult to let XML
description to force the client side programming platform generate code
which follow a logical rule. So far I haven't found such a means in xml
schema. For example, you can add some xml annotation/comments to indicate
the client side that what's the expected value/default value for the
certain field, however, it is still depend on the client-side's proxy
implementation (whether it adopt the annotation or WSDL description or
not).

So far I think the most guranteed means is still make sure both sides using
.net framework (actually this is the preferred approach for WCF ) so that
we can enforce such logical rules via code level definition such as default
value (or constructor initialization).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Jul 22 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Wade Redmond | last post: by
2 posts views Thread by Arvind Ganesan | last post: by
10 posts views Thread by =?Utf-8?B?SmFtZXMgV29uZw==?= | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.