Silverlight, WPF, ASP.NET

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

Friday, February 09, 2007 5:50:25 AM (Eastern Standard Time, UTC-05: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  | Comments [2] 


WCF: This collection already contains an address with scheme http

Wednesday, January 24, 2007 11:30:30 PM (Eastern Standard Time, UTC-05:00)

The Problem:

I recently ran into this problem issue trying to host a service through my shared web hosting service provider.

Server Error in '/' Application.

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item

The Solution:

The solution, since WCF services hosted in IIS can have only one Base Address was to create a custom service factory to intercept and remove the additional unwanted base addresses that IIS was providing. In my case IIS was providing:

domain.com
www.domain.com
dedicatedserver1234.hostingcompany.com

We are able to customize our .svc file to specif a custom serive factory

<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"
Factory="Microsoft.ServiceModel.Samples.CustomHostFactory" %>

We are then able to create our custom factory by inheriting from ServiceHostFactory and overriding as required

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

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

In my case I decided to pass through baseAddresses[1] (which was the www. address) but it would probably be wise to specify that address you want to prevent changes by your web host from impacting yoru code.

Big thanks to those at WebHost4Life that put the time into helping me resolve this.

 

Technorati tags:

 

 

Posted in  | Comments [13] 


Thank You Juval Löwy and INETA

Thursday, January 11, 2007 7:39:48 PM (Eastern Standard Time, UTC-05:00)

A BIG thank you goes out to INETA for sponsoring a visit by Juval  Löwy of IDesign to speak to the Triangle .Net User Group (TRINUG) on the Windows Communication Foundation (

). It was an excellent presentation that left us all wanting to attend his master class and get our hands on his upcoming book! (Which is currently available thorugh Safari Rough Cuts)

Juval Löwy

Thanks!

 

Technorati tags: , ,

Posted in  |   | Comments [0] 


April TRNUG Meeting Review: Indigo

Thursday, April 15, 2004 11:24:46 PM (Eastern Daylight Time, UTC-04:00)

This months guest speaker was Doug Turnure, Developer Evangelist, Microsoft and shared a great look at the "Vision" of Indigo and how we've moved from the start of object oriented programming to the service oriented models that are being put in place today. With the builds in flux as they are since PDC, Doug was unable to show us any live code, but it sounds like the Indigo team is on top of things. There was some good insight into the importance of standards and why Microsoft was waiting on some of them. (Microsoft waiting for standards? Haven't written that much in the past, but I think it's a great sign)

 

Some key points on Indigo:

 

Services Are Autonomous

Boundaries are explicit

Share schema, not class

Policy-based compatibility

 

If any of you know Doug, check with him next time you see him and verify if he really encrypted the notes he passed to the girls in school so the teacher wouldn't read them out loud and embarrass him!

 

It was a great presentation we look forward to seeing Doug back in the Triangle soon.

 

NOTE - The next MSDN event in Raleigh is on May 4th!

An audio replay of the WSV203 session from PDC that Doug referenced is available here and the powerpoint slides are available here. (If you're interested, all the PDC sessions are available here)

Posted in  |  |   | Comments [4]