WCF .svc Gives HTTP 404 on Host, Works Perfectly on Local

by ashic 30. September 2008 19:19

I was recently working on a project that used WCF web services extensively. Everything was ready for deployment, I deployed to the remote host, and voila – nothing works (nothing WCF related). After a LOT of frustration, I managed to make it work. I found two areas which were causing the problem:

1. The .svc extension was NOT setup right at our host. You can set this in the web.config webhandlers section for IIS 7, but not for IIS 6.0. I had IIS 7, but decided to bug my host to set up the extension on the server ;)

2. I had multiple host headers; i.e. my site worked for both http://mysite.com and http://www.mysite.com. To resolve this issue, you can either disable all but one if your host permits. Otherwise, you need to use a factory. Here’s an example of such a factory:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace Shipping.WebService
{
    public class MultipleIISBindingSupportServiceHostFactory : ServiceHostFactory
    {

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {

            Uri[] requiredAddress = GetAppropriateBase(baseAddresses);

            return base.CreateServiceHost(serviceType, requiredAddress);

        }

        Uri[] GetAppropriateBase(Uri[] baseAddresses)
        {
            List<Uri> retAddress = new List<Uri>();
            retAddress.Add(baseAddresses[0]);

            return retAddress.ToArray();

        }

    }

}

Granted, it can be way more efficient and you can do nifty tricks with the url to support both http:// and http://www, but I can happy to have just one for my webservice.

Next, you need to set the factory in the markup of the ,svc file. You can do it like this:

<%@ ServiceHost Language="C#" Debug="false" Factory="Shipping.WebService.MultipleIISBindingSupportServiceHostFactory" Service="Shipping.WebService.ClientService" CodeBehind="ClientService.svc.cs" %>

Hope that helps.

Share or Bookmark this post…
  • Facebook
  • DotNetKicks
  • Digg
  • LinkedIn
  • Technorati
  • del.icio.us
  • Google
  • Live
  • Tumblr
  • msdn Social
  • Ping.fm
  • Reddit
  • Slashdot
  • StumbleUpon
  • TwitThis
Categories: .NET | AJAX | ASP.NET

Powered by BlogEngine.NET 1.6.1.0
Theme by Ashic Mahtab

Need an expert?

Ashic Mahtab
ashic@live.com
(+44) 07879927393

Stats

Featured Ads

 

Donations

I maintain this site and create all content entirely in my own time just to help you guys out. If you find the stuff helpful, or cool or just like what you see, I'd appreciate you chipping in to help out with the hosting costs. It's easy to do so - just click the button below - no amount is too low :)