Home > WCF > Adding Attributes On Generated WCF Contracts

Adding Attributes On Generated WCF Contracts

We saw on previous posts (i.e. The Smart Client Object Model) that CodeFluent Entities fully supports WCF, and by that I don’t mean that the generated classes are just serializable (which they are by the way), but instead I mean that CodeFluent Entities can generate the entire communication layer (services, contracts, proxies and configuration) so you can spend your time somewhere else.

A question that we often get is:

Great it generates all my contracts, but what if I’d like to have some custom attribute on a specific operation in one of my contracts? How can I do that?

To a simple question, a simple answer!

Using CodeFluent Entities it’s possible to add attributes on generated elements via the cf:attribute node. Likewise you can specify attribute parameters using the cf:argument node.

For instance, say I’d like to add the following WebInvoke attribute on a LoadAll method:

[WebInvoke(UriTemplate = "LoadAll", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
CustomerCollection LoadAll();

Here’s how to do it in XML in my model:

<cf:method name=”LoadAll” body=”load”>
<cf:attribute name=”System.ServiceModel.Web.WebInvoke” class=”_serviceModel:serviceInterface”>
<cf:argument name=”UriTemplate” expression=”&quot;{2:Name}&quot;” />
<cf:argument name=”Method” expression=”&quot;Post&quot;” />
<cf:argument name=”BodyStyle” expression=”System.ServiceModel.Web.WebMessageBodyStyle.Wrapped” />
<cf:argument name=”ResponseFormat” expression=”System.ServiceModel.Web.WebMessageFormat.Json” />

Note 1: _serviceModel:serviceInterface is a filter which indicates the producer to place this attribute on each method of the service interface. Other possible filters are:_serviceModel:service (points to the service class), _serviceModel:serviceInterface (points to the interface), _serviceModel:serviceInterfaceProxy (points to the proxy interface).

Note 2: Escape quotes using ‘&quot;’ to define static strings.

Note 3: The expression attribute of the argument element understands 3 aliases: {0} being the default namespace, {1} being the parent node (attribute), {2} being the parent of the parent (method in this case).

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s