Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate the .svc in the URL of a WCF 4 service using Routes?

Tags:

rest

wcf

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?

like image 782
BrettRobi Avatar asked Jul 20 '10 18:07

BrettRobi


People also ask

Where is .SVC file in WCF?

svc File for the WCF Service. WCF services hosted in IIS are represented as special content files (. svc files) inside the IIS application.

What is .SVC file?

Text file that contains information about a Windows Communication Foundation (WCF) service that can be run using Microsoft Internet Information Services (IIS); includes a WCF-specific processing directive that activates hosted services in response to incoming messages.

How do I open a SVC file in my browser?

Solution: Open IIS Manager (Start > Run > search "inetmgr"). Browse from the top level server to Sites and expand the Default Web Site. Select each of the Revit Server applications, and in the Content View, right click on the SVC file and select Browse.


2 Answers

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="[subfolder/]filename.svc" or "~/[subfolder/]filename.svc" 
             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!

like image 50
marc_s Avatar answered Oct 12 '22 14:10

marc_s


Just to wrap up the answer and add one more point there. You'll need the same ASP.NET route registration as above:

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))); 
}

In order to get this working you need however to add some more things to web.config. The service hosting should be configured to be ASP.NET compatible. This can be done by adding aspNetCompatibiliyEnabled="true" to serviceHostingEnvironment element:

 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

Hope this clarifies and gives an easier to find solution.

like image 26
tomij Avatar answered Oct 12 '22 13:10

tomij