IServiceDescription is feeling lonely

Topics: Web Api
Jun 11, 2011 at 5:49 PM

In the previous preview I was able to manipulate the service description through the config by implementing IServiceConfiguration (but see http://wcf.codeplex.com/discussions/244369 - it never quite worked). I notice IServiceConfiguration is still around in the latest preview, but I can't see how to add an instance of something implementing IServiceConfiguration to the config, or more generally, how to manipulate the service description when configuring an app (HttpHostConfiguration no longer provides for adding an IServiceDescription).

Is this an oversight, or a purposeful restriction?

Thanks

ian

Coordinator
Jun 14, 2011 at 1:04 AM

I pulled it for now as it was causing a bit of confusion. To accomplish the same thing just derive from HttpConfigurableServiceHostFactory with a custom host. Then override the CreateService method and grab the description right off of the host to tweak it before you return it. You will access the Description property on the host.

Jun 14, 2011 at 2:03 AM

Hi Glenn

Thanks for the reply. I kinda knew that was the fallback option. I guess that's what I'm going to do here.

Kind regards

ian

Coordinator
Jun 14, 2011 at 2:23 AM

Sure. There is an overload of MapServiceRoute that accepts a generic type for a factory. You might want to use that.

Coordinator
Jun 14, 2011 at 2:30 AM

Just as a side note is we are at the point we are looking to actually move the code-based config work into the product. As we design that we will keep in mind this scenario.

Thanks

Jun 14, 2011 at 2:44 AM

Just to provide a bit of context around my use case (most of which you've seen already).

I've created a UriTemplateAttribute attribute that can be attached to resource classes. UriTemplateAttribute takes a prefix and the remainder of a uri template path:

 

[UriTemplate("users", "{id}")]
[ServiceContract]
public class User
{}

I use UriTemplateAttribute in several ways at app startup:

1) The app reflects over all the classes attributed with UriTemplateAttribute, and builds a UriFactory, which can then be used for building relative and absolute URIs, and deriving base URIs.

2) I register all the classes attributed with UriTemplateAttribute in the RouteTable based on the prefix value of the attribute.

3) I also use the info in UriTemplateAttribute to modify the UriTemplate parameters of all WebGet and WebInvoke attributes. Hence the need to modify the service description. (This assumes, of course, that all the methods for a resource class share the same UriTemplate.)

This then allows me to have DRY URIs: there's only once place in the code I need change the URI details for a particular resource - the UriTemplateAttribute attached to that resource's class.

ian

Coordinator
Jun 14, 2011 at 3:11 AM
Why not just make it a service behavior? If your attribute is an IServiceBehavior you can modify the description.

Sent from my Windows Phone

From: iansrobinson
Sent: Monday, June 13, 2011 6:45 PM
To: Glenn Block
Subject: Re: IServiceDescription is feeling lonely [wcf:261041]

From: iansrobinson

Just to provide a bit of context around my use case (most of which you've seen already).

I've created a UriTemplateAttribute attribute that can be attached to resource classes. UriTemplateAttribute takes a prefix and the remainder of a uri template path:

[UriTemplate("users", "{id}")]
[ServiceContract]
public class User
{}

I use UriTemplateAttribute in several ways at app startup:

1) The app reflects over all the classes attributed with UriTemplateAttribute, and builds a UriFactory, which can then be used for building relative and absolute URIs, and deriving base URIs.

2) I register all the classes attributed with UriTemplateAttribute in the RouteTable based on the prefix value of the attribute.

3) I also use the info in UriTemplateAttribute to modify the UriTemplate parameters of all WebGet and WebInvoke attributes. Hence the need to modify the service description. (This assumes, of course, that all the methods for a resource class share the same UriTemplate.)

This then allows me to have DRY URIs: there's only once place in the code I need change the URI details for a particular resource - the UriTemplateAttribute attached to that resource's class.

ian

Jun 14, 2011 at 4:34 AM

Because I want to make it hard for myself :)

Of course - a service behavior it is.

Thanks

ian