WCF Tip: Using Properties in Service Contracts

Skill

WCF Tip: Using Properties in Service Contracts

Posted in:

If you check out any of our previous WCF Tutorials, you'll notice all of the Service Contracts are implemented with interfaces containing only methods. For the longest time I thought that's all that was supported, until I thought about it a little more. In C# land, properties are simply wrappers around a get method and set method, so why wouldn't we be able to use properties in a service contract? Well the answer is we can.

Let's start by checking out the service contract we created for our interprocess communication tutorial.

[ServiceContract]
public interface IStringReverser
{
  [OperationContract]
  string ReverseString(string value);
}

Let's add a property to this interface that retrieves the last string that was reversed.

[ServiceContract]
public interface IStringReverser
{
  string LastReversedString
  {
    get;
  }

  [OperationContract]
  string ReverseString(string value);
}

Since the property doesn't have the OperationContract attribute, it won't be available through a WCF Service. You might initially think to just throw the attribute above the property, but doing so would result in this compile-time error:

Attribute 'OperationContract' is not valid on this declaration type. It is only valid on 'method' declarations.

That makes sense, since the property is not a method. What is a method, though, is the get portion of that property. This means, all we have to do is move the attribute to above the get keyword.

[ServiceContract]
public interface IStringReverser
{
  string LastReversedString
  {
    [OperationContract]
    get;
  }

  [OperationContract]
  string ReverseString(string value);
}

And there you have it, you now have a WCF service contract using properties. It's definitely better than having to implement separate get and set methods for each value.

Anonymous
08/11/2009 - 17:06

Thanks a lot dude. That small little trick worked out great.

You rock!

reply

ben
09/09/2009 - 15:28

thanks alot i's usefull:)

reply

Anonymous
10/13/2009 - 02:00

very useful ,thanks

reply

ziziler
05/03/2010 - 11:34

good

reply

netDeveloper
06/30/2010 - 02:33

Thats great. This is a very useful article
Thanks

reply

Chandrakant
09/09/2010 - 01:41

Rokz

reply

AlexK
11/25/2010 - 05:42

thanks a lot :)

reply

Detlef
12/09/2010 - 16:02

Splendid.

reply

Anonymous
12/20/2010 - 07:10

Is property set all applying the same rule?

reply

Anonymous
01/13/2011 - 15:42

This is great for the get method. Can you show an example of how to set a property before calling a method in the service? Tried using a set but the value is blank in the service this.myProperty always is blank.

reply

Anonymous
02/02/2011 - 05:03

Thanks

reply

Anonymous
05/25/2011 - 09:45

Good to know! Thanks.

reply

michel
10/13/2011 - 08:15

Tks Man.Good one ..

reply

Anonymous
12/23/2011 - 09:51

thx!

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.