The default Global.asax.cs file from the "WCF REST Template 40(CS)" project template and every tutorial I've seen online include a variation of the following method:
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
}
Managing the service routing in this way seems needlessly cumbersome when the WebApplication
itself should be able to discover which services should be available and apply routes based on convention or metadata.
QUESTIONS
- Is there a built-in way beyond the default to define the service routes (either configurable in the web.config, or compiled onto the service itself)?
- Do others that use this template always follow the model provided or has someone else come up with a better approach?
Proposed Solution
Migrated my proposed solution to an answer
I guess I have to assume that silence is acceptance. Here is my solution (originally from my question):
Assuming there is nothing better built in or otherwise available (because I didn't find anything), my attempt at doing this involves defining an attribute:
which is used to decorate each service contract that should be exposed, and changing the default
RegisterRoutes
to:This seems to work and isn't too intrusive because it only happens at
Application_Start
, but I'm new to building RESTful web services with WCF4 so I don't know what sort of problems it could cause.If anyone comes up with a more elegant way of solving this, I'd gladly consider any alternative.