Question

I've got WCF ServiceContract with properties alike:

int P1 { [OperationContract()] get; [OperationContract()] set; }

And I need the same on client but generated protocol gives me:

get_P1() and set_P1(int) methods so I need every-time manually create another class interface alike:

int P1 { get { return get_P1() } ; set { et_P1(value) ; }

How can I automate such class creation?

Reason: The only thing I want to edit is interface and realization, all other things I want to be generated automatically.

Was it helpful?

Solution

You can change how the tool (svcutil or "Add Service Reference") will generate your contracts, but it won't be simple. You'll need to implement a WSDL Import and a code generation extension and then hook it up to the tool to convert between the two methods and a property. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/10/18/wcf-extensibility-wsdl-import-and-code-generation-extensions.aspx has some information about those extensions.

Another alternative would be to share the contract between the server and the client - instead of letting the tool generate a client proxy, you simply copy (or reference) the contract file in the client project.

So, you can do that. But I don't think you should. Properties are commonly associated with simple accessor for the object state. Having an [OperationContract] property means that the act of accessing a property now has a potential cost of a full network round-trip, plus any addition serialization / packaging / framing that it entails. Methods don't have that implicit requirement, as they can (and often do) perform multiple tasks, including calling other methods. Even if it makes sense at this moment to have an operation contract in the property accessors, it won't be natural to hardly anyone new to the code, so it may lead to maintenance problems in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top