I was writing integration test for WCF service and needed to host my service:

    [TestFixture]
    public class RealHostTests
    {         
        private ServiceHost _host;        
        private Uri _baseAddress = new Uri("http://localhost:8080/dev");
        private Client.ServiceClient _client;

        [SetUp]
        public void Setup()
        {
            _host = new ServiceHost(typeof(PayService), _baseAddress);
            _host.Open();

            _client = new Client.ServiceClient("BasicHttpBinding_IService", new EndpointAddress(_baseAddress));
        }

        [TearDown]
        public void TearDown()
        {
            _client.Close();
            _host.Close();           
        }

        [Test]
        public void ServiceViaRealHost_Ping_CanPing()
        {
            PingResponse response = _client.Ping(new PingRequest { Echo = "Hi" });
            Assert.IsNotNull(response);
            Assert.AreEqual("Hi", response.Echo);
        }

Because of the relatively new http.sys security this call was failing with "Your process does not have access rights to this namespace...".

Knowing that Visual Studio provides DesignTimeAddress endpoint I replaced http://localhost/:8080/dev to localhost:8733/DesignTimeAddresses/dev
and the unit test began working under debugger.

However when I ran the same unit test in normal mode, under NUnit test runner, the test was failing with

The HTTP service located at http://localhost:8733/Design_Time_Addresses is unavailable. This could be because the service is too busy or ... System.Net.WebException : The remote server returned an error: (503) Server Unavailable.

Here is what I found out after some research.

  1. The service was working in debug mode because There is a WCF project property "Start WCF Service Host when debugging another project in the same solution". This property was checked. During unit test debugging WCF test client was launched. The interesting part was that _host = new ServiceHost(typeof(MyService), _baseAddress) initialized host to the just launched instance of WCF test client as opposed to creating a new service host instance. When unit test ran outside of the debugger WCF test client wasn't launched and call to ServiceHost was simply failing.

  2. The endpoint address should be http://localhost:8733/DesignTimeAddresses/
    as opposed to
    http://localhost:8733/DesignTimeAddresses/dev even though default scafolding WCF service library template generates it with trailing slash and service name (/Service).

  3. It is not too hard to register a new URI one time with

netsh http add urlacl url=http://+:8733/Design_Time_Addresses/ user=Everyone