tags:

views:

88

answers:

1

I'm using WCF 4 on IIS 7.5 and want to eliminate the default .svc extension from the URL of all my RESTful services. I've seen the approaches documented using the Url Rewrite Module and an IHttpModule but I don't want to take those approaches.

I am vaguely familiar with the concept of Routes introduced in ASP.NET MVC and as I understand they are now abstracted out of MVC in Net 4 as System.Web.Routing. But in looking at the docs it appears I need to add a Global.asax file to my project which I'm not really keen on. Is there any other way to handle this?

I've also seen the Configuration-Based activation feature, but that only seems to eliminate the .svc file, but still requires I use .svc in the url of my service.

Can anyone summarize my options here for not needing .svc in my urls?

+2  A: 

Sure, no problem: first off, read all about the new WCF 4 features in A Developer's Introduction to Windows Communication Foundation 4.

What you're looking for is called file-less service activation. It's a new setting in your <system.serviceModel> that looks something like this:

<serviceHostingEnvironment> 
    <serviceActivations>
        <add factory="System.ServiceModel.Activation.ServiceHostFactory" 
             relativeAddress="/YourService" 
             service="SomeNamespace.YourService"/>
    </serviceActivations>
</serviceHostingEnvironment>

Basically, all the information you'd have in the *.svc file (path, service to call) is in this config section now.

You should be able to call this service at

http://yourserver/virtualdirectory/YourService

now - no more *.svc, no messy URL rewriting etc. - it just plain works!

Update: it doesn't seem to work that well, unless you go in and add a *.svc extension to your relative path - kind defeats the whole purpose!

If you want to register using an ASP.NET route, check out the MSDN docs on that topic. You'd have to have something like this in your application startup, in a web app that would be global.asax.cs:

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

private void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new ServiceRoute("YourService", 
               new WebServiceHostFactory(), typeof(SomeNamespace.YourService))); 
}

Hopefully, with that, you'll be able to get your service up and running without any *.svc file extensions!

marc_s
It looks like you're talking about using Configuration-Based Activation (http://msdn.microsoft.com/en-us/library/ee358764.aspx) rather than System.Web.Routing. The problem is that it appears relativeAddress must use a WCF supported extension (such as .svc). I can't get your example to work, I just get the error: 'The registered relativeAddress 'YourService' under section 'system.serviceModel/serviceHostingEnvironment/serviceActivations' in configuration file does not have an extension'
BrettRobi
@BrettRobi: funny - that MSDN article contradicts itself.... and this is contrary to all the presentations and talks I've seen/heard so far on file-less activation..... I've updated my answer anyway
marc_s
Yeah I was hopeful it would do the trick but it really looks like a simple way to eliminate the .svc file itself but not the need for .svc in the url. Thanks for the help though...
BrettRobi