pistacchio
pistacchio

Reputation: 58863

Do WCF Services Expose Properties?

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

Upvotes: 12

Views: 18637

Answers (6)

IraW
IraW

Reputation: 53

Perhaps things have changed since this question was initially asked, but on .NET 4.8 you can expose properties.

The interface declaration looks like this.

    string CapturePath 
    {
        [OperationContract]
        get;
        [OperationContract(IsOneWay = true)]
        set; 
    }

The caller uses it just like any property, such as:

            _captureThing.CapturePath = "test";
            LogMessage(_captureThing.CapturePath);

Upvotes: 0

KKR
KKR

Reputation: 79

PLEASE PLEASE don't expose Properties as a web method.. This will not work in HTTPS. I had BIG time identifying and fixing this issue. The best way is to write a concrete method to return in WCF.

Upvotes: 2

Rishi_raj
Rishi_raj

Reputation: 85

You can expose properties but u will have to use [DataContract] attribute and can declare property as [DataMember] attribute for properties.

Upvotes: 4

Steve Dignan
Steve Dignan

Reputation: 8530

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();

Upvotes: 22

Pontus Gagge
Pontus Gagge

Reputation: 17258

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.

Upvotes: 4

John Saunders
John Saunders

Reputation: 161773

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

Upvotes: 14

Related Questions