I've implemented an AJAX application that uses a WCF service to avoid server round trips. Accessing the service using http://www.planebustrain.com/UserService.svcworks, but accessing without "www", using http://planebustrain.com/UserService.svcdoes not. This means that accessing my application with http://www.planebustrain.comworks properly, with client-side AJAX code accessing the WCF service correctly, but accessing the application with http://planebustrain.commeans that the user service is unavailable and the client-side application code falls back to performing a page resubmit. How can I fix this?
Unfortunately this is how WCF works. The prefix will dictate what URL will have to be used to call on the service. So if you have... <add prefix="http://www.planebustrain.com"/> ...you will only be able to use http://www.planebustrain.com/UserService.svc
I added <baseAddressPrefixFilters> <add prefix="http://www.planebustrain.com"/> </baseAddressPrefixFilters> to the config file to make WCF happy and I'm using if (!Request.Url.DnsSafeHost.StartsWith("www")) Response.Redirect("http://www.planebustrain.com"); in default.aspx, which ensures that only the "www" domain is used. It works, but is there are more elegant way of doing it or is this as good as it gets?
In .Net 4 there's a "multipleSiteBindingsEnabled" element that should allow you to call on the WCF application using 'www' and without 'www'. I encourage you to monitor the forum. As soon as we fully enable .Net 4.0 our servers, we will immeidately post it in the forum.
An easy way around this (one that I use anyway) is to use the URL rewrite module to map multiple urls to the single binding. domain.com/services/service.svc www.domain.com/services/service.svc I might setup a rule where '/services/.*' remaps to 'http://www.domain.com/services/' explicitly and add back in the capture groups. Url Rewrite is a really powerful tool. Side Note: A nice side effect is that all my services can now be hit from 'http://services.domain.com/{service-name} and any attempts to hit 'http://domain.com/services' or 'http://www.domain.com/services' will redirect to 'http://services.domain.com' which is then rewritten behind the scenes to 'http://www.domain.com/services/service.svc' to hit the target service. Again just an option if you would rather set this from IIS instead of in code.