开发者

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

I'm using WCF 4 on IIS 7.5 and want to eliminate the default .svc extension from the URL of all my开发者_如何学JAVA 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?


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!


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.


Important clarification for IIS 7.5 and Framework 4.0:

For the Web routing module to work, it requires the application pool set to "Integrated Mode", it doesn't works in "Classic Mode"

One additional problem I've found after switching is that when in "Integrated Mode" the application crashed on startup because I had modules included in the <system.Web> section.

I fixed it by moving the modules to the <system.webServer> configuration section that is new for IIS 7

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜