tags:

views:

465

answers:

3

Hi, In the interface required to implement a WCF service, I declare the main class with the [ServiceContract()] attribute and any exposed method with [OperationContract()].

How can i expose public properties? Thanks

+4  A: 

You can't. That's not how it works. Methods only.

John Saunders
Indeed. Just to expand (without the need for another answer) - it is an RPC stack - not a remoting stack. If you want properties, return an object from a method, and look at the properties of the object at the client.
Marc Gravell
I'll agree with Marc, but clarify: it's not WCF that's the RPC stack; it's SOAP+WSDL standards that are. All they can describe is operations, sent in a message. There's not even the concept of an instance, so what would you be getting properties _from_?
John Saunders
+1  A: 

Properties are an object oriented aspect of component interfaces. WCF is about services, where you have to think about and design the sequence of interactions between your components.

Object orientation does not scale well to distributed scenarios (where your code executes on multiple servers or even in multiple processes) due to the cost of roundtrips, potentially expensive state management, and versioning challenges. However, OO is still a good way of designing the internals of services, especially if they are complex.

Pontus Gagge
+1  A: 

Since the get portion of a property is a method, this will technically work but, as mentioned in previous answers/comments, this may not be advisable; just posting it here for general knowledge.

Service Contract:

[ServiceContract]
public interface IService1
{
    string Name
    {
        [OperationContract]
        get;
    }
}

Service:

public class Service1 : IService1
{
    public string Name
    {
        get { return "Steve"; }
    }
}

To access from your client code:

var client = new Service1Client();
var name = client.get_Name();
Steve Dignan