Sunday, 24 February 2013

URL Rewriting for WCF service

How do you consume a IIS hosted WCF service without referring to the .svc file in the URL? Say, a service URL is http://localhost/Test/Test.svc and I want to access it using http://localhost/Test

One of the solutions is described here :

1. Enable file less activation in WCF service.
2. Host the WCF service in aspNetCompatibility mode
3. Add Global.asax file to the project
4. Add routing handler to web.config by referring System.Web.Routing
5. Add a route mapping in Application_Start method of Global.asax using ServiceRoute class present in System.ServiceModel.Activation

I came across a similar problem where I had to access my WCF service using a URL that followed a pattern like - http://localhost/Test/X where X is not a constant value and is added by the caller at runtime. Also, the value of X is expected to the used inside the implementation of the invoked WCF method.

The above mentioned approach did not work directly for my case owing to a known limitation. So instead of using the ASP.NET routing as a means, I looked at IIS routing. Since our WCF service is going to be hosted in Integrated Mode in IIS, thus there were not many roadblocks. So here is what I did:

1. Installed Web Platform Installer on my machine. It is an extremely cool utility that allows me to search for useful components, applications and products that can be consumed readily.
2. Installed "IIS URL Rewrite Module 2" by selecting URL Rewrite 2.0 in web platform installer. More details about the capabilities of the modules are available here
 

3. Modify the web.config of the WCF service application to include a rewrite rule. The rule uses a regular expression to convert the part of URL present after the application name into a querystring.
 
With this rule in place the URL http://localhost/Test/X to http://localhost/Test/Test.svc?q=X

4. To access the querystring values in the WCF service method, I enabled aspNetCompatibilityMode also.

And we are done.
 
UPDATE (4/2/2013): One more example can be when you want URL rewrite module to handle clean urls like "products/category/10/product/20" and translate to "products.aspx?category=10&product=20". You can use following rule for that -
 
 
 
 
You can try advanced scenario by launching the URL rewrite module from IIS - It has a pretty helpful wizard and makes url rewriting a breeze.

4 comments:

  1. Thanks a lot.
    Can you give another example for accessing the querystring values?

    ReplyDelete
    Replies
    1. Updated with one more example. Hope that helps.

      Delete
  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Windows foundation communication, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Windows foundation communication. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete
  3. Thank you for sharing such an informative article. I really hope I can see other interesting posts. Keep up the good work!


    Melbourne App Developer

    ReplyDelete