WCF: This collection already contains an address with scheme http (Update)

Friday, February 09, 2007 2:50:25 AM (GMT Standard Time, UTC+00:00)

Thanks to those of you that have sent me comments on my CustomHostFactory post. I wanted to post publicly question that I received from a couple of people.

While specifying a hard coded BaseAddress array works, it's not really practice for deploying applications in multiple environments and updating configuration changes in the future. I set out to find a better approach to this, and found a blog post by Aaron Skonnard here that add a simple configuration capability by reading the following section of a web.config file.

  <appSettings>
    <add key ="baseHttpAddress" value=http://localhost/ServiceTest/service.svc/>
  </appSettings>

My updated CustomHostFactory code is as follows

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.Configuration;

    class CustomHostFactory : ServiceHostFactory 
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            CustomHost customServiceHost =
                new TradeServiceCustomHost(serviceType, baseAddresses);
            return customServiceHost;
        }
    }

    class CustomHost : ServiceHost
    {
        public CustomHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, GetBaseAddresses())
        { }
        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();
        }

        // read base addresses from AppSettings in config
        // Added from sample at http://pluralsight.com/blogs/aaron/archive/2006/04/19/22106.aspx
        private static Uri[] GetBaseAddresses()
        {
            List<Uri> addresses = new List<Uri>();
            AddBaseAddress(addresses, "baseHttpAddress");
            return addresses.ToArray();
        }
        private static void AddBaseAddress(List<Uri> addresses, string key)
        {
            string address = ConfigurationManager.AppSettings[key];
            if (null != address)
                addresses.Add(new Uri(address));
        }
    }
 

It would be really great if there was a way to remove base address through the web.config similar to how asp.net lets you remove providers and other settings with something to the effect of

<BaseAddresses>
    <Remove All />
    <Add BaseAddress="http;//..." />
</BaseAddresses>

Technorati tags:

Posted in wcf  | Comments [2] 


Monday, January 21, 2008 5:20:31 PM (GMT Standard Time, UTC+00:00)
I don't think I am understanding how this is supposed to work. I am calling my service from Java Script and it works if the base address is domain.com and the user navigates to the site using domain.com. It doesn't work if the user navigates to the site using www.domain.com. I thought setting up a custom host factory is supposed to solve this problem? Am I missing something? What about https?
Curtis
Friday, June 20, 2008 7:27:11 PM (GMT Daylight Time, UTC+01:00)
Rob,

Thanks for posting on this issue. Saved me a lot of time zeroing in on the problem.

You'll find the config file solution your looking for here:
http://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.baseaddressprefixfilterelement.aspx

Zac
zac morris
Comments are closed.