How to Install a WCF Service on a GoDaddy hosted site

June 24, 2009 15:26

Introduction

I've been engaged in the creation of a Silverlight TagCloud control of late. The architecture is such that I have the Silverlight control on the front-end, which communicates with a WCF service that in turn retrieves tag data from my blogging engine, BlogEngine.NET. I've completed development and now, as a first deployment step for the overall project, would like to deploy the WCF service to my site hosted by GoDaddy.com.

For purposes of this post I'm assuming you're at a similar point where you want to toss the web service over the wall and see what problems, if any, you run across in a "real" environment.

Deployment of the Service/The Problem

To that end, I identified the two files I needed to copy over to deploy the service: ~\TagCloudService.svc in my web site's root and ~\App_Code\TagCloudService.svc.cs, which was created for me in the App_Code folder when I right-clicked on my web site in VS2008, chose to add a new item, and selected "Silverlight-enabled WCF Service". (If you need some assistance getting this far, check out Step By Step - Using Silverlight to Access a WCF Service Hosted In a Console Application).

I FTP'ed the files to my site, fired up my browser, typed in the service's url, and promptly got this error:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection. Parameter name: item

In graphical format:

image

I wasn't expecting complete success right off the bat…

The Solution

So, I started digging, and almost immediately found this post (scroll down to the section sub-titled "WCF Services on web hosts with Multiple IIS Identities (GoDaddy)"), which provides a brief summation of the problem:

WCF services hosted on IIS can only have one base address. Under some hosting configurations, such as GoDaddy's shared Windows Economy Plan, multiple base addresses exist. When attempting to access WCF services on an IIS with multiple base addresses, an exception […] occurs.

That post coupled with this one proved to be of great help in coming up with the first solution to this problem. This one provided an alternative where you only need to modify the web.config file. I'll go over both approaches.

1.) Coding solution

For the code solution, I'm going to detail the very few steps I had to go through to get this to work in my situation. Your mileage may vary, so take a look at the other posts if what I go over here leaves you stuck as they detail some other things I didn't have to deal with.

First, open your .svc file. Add the "Factory" attribute to the ServiceHost declaration, replacing the web project name with your own:

 

   1: <%@ ServiceHost Language="C#" Debug="true" Service="TagCloud.Web.TagCloudService"
   2:     CodeBehind="~/App_Code/TagCloudService.svc.cs" Factory="TagCloud.Web.CustomHostFactory" %>

 

Now, in the service's code-behind file, add the following new class alongside your service class:

 

   1: class CustomHostFactory : ServiceHostFactory
   2: {
   3:     protected override ServiceHost CreateServiceHost (Type serviceType, Uri[] baseAddresses)
   4:     {
   5:         // If more than one base address exists then return the second address,
   6:         // otherwise return the first address
   7:         if (baseAddresses.Length > 1)
   8:         {
   9:             return new ServiceHost (serviceType, baseAddresses[1]);
  10:         }
  11:         else
  12:         {
  13:             return new ServiceHost (serviceType, baseAddresses[0]);
  14:         }
  15:     }
  16: }
  17:  
  18: class CustomHost : ServiceHost
  19: {
  20:     public CustomHost (Type serviceType, params Uri[] baseAddresses)
  21:         : base (serviceType, baseAddresses)
  22:     { }
  23: }

 

This has the effect of returning one and only one ServiceHost base address, thus satisfying IIS.

Re-deploy the changed files to your site, type in the web service url, and you should see confirmation that your WCF web service is good to go:

image

2.) Web.config solution

This one is especially useful if you start getting "404 – Resource not found" errors when trying to access the web service via the browser. I found I was getting this on a sub-site hosted on the server space of another GoDaddy web site of mine. I found that if I removed the "Factory" attribute from the ServiceHost declaration then I no longer got the "resource not found" error, but then I was back to square one on the original problem. So, modifying the web.config as follows was the solution to that particular issue.

Open your web app's web.config file and add the following to the system.servicemodel section:

   1: <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
   2:     <baseAddressPrefixFilters>
   3:         <add prefix="http://www.itscodingtime.com"/>
   4:     </baseAddressPrefixFilters>
   5: </serviceHostingEnvironment>

Of course, change the add prefix to your own site's URL and you're done.

Conclusion

That's it. I know I didn't go into a lot of depth on why IIS only wants one base address and such, but there are plenty of other sources out there that can provide a better explanation than I can I've no doubt. I just want to get back to my code and have this process documented for the next time you or I run across it.

References

WCF Services on web hosts with Multiple IIS Identities (GoDaddy)

WCF: This collection already contains an address with scheme http

WCF error: "This collection already contains an address with scheme http"


Comments

All comments are moderated and require approval before display. Thanks for your understanding.

Add comment


 


[b][/b] - [i][/i] - [u][/u] - [q][/q]



Preview