tags:

views:

15

answers:

0

I'm trying to test some WCF extension stuff I've written, but I can't even get the basic WCF service up and running in my test class. The code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.ServiceModel;
using System.Threading;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.Net;
using System.IO;

namespace Test
{
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        bool TestMethod();
    }

    public class FakeService : ITestService
    {
        public bool TestMethod()
        {
            return true;
        }
    }

    [TestFixture]
    public class UnitOfWorkExtension
    {
        [Test]
        public void UnitOfWorkLifecycleIsManagedPropertly()
        {
            var baseAddress = new Uri("http://localhost:9090/testservice/");
            var host = new ServiceHost(typeof(FakeService), baseAddress);
            var mexBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            var debugBehavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
            var metaBinding = MetadataExchangeBindings.CreateMexHttpBinding();

            if (mexBehavior == null)                                           
            {
                mexBehavior = new ServiceMetadataBehavior();
                host.Description.Behaviors.Add(mexBehavior);
            }

            if (debugBehavior == null)
            {
                debugBehavior = new ServiceDebugBehavior();
                host.Description.Behaviors.Add(debugBehavior);
            }

            mexBehavior.HttpGetEnabled = true;
            debugBehavior.IncludeExceptionDetailInFaults = true;
            host.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(), "test");
            host.AddServiceEndpoint(typeof(ITestService), metaBinding, "mex");
            host.Open();

            try
            {
                var wc = new WebClient();
                System.Diagnostics.Debug.WriteLine(wc.DownloadString(new Uri(baseAddress, "test")));
                System.Diagnostics.Debug.WriteLine(wc.DownloadString(new Uri(baseAddress, "mex")));
            }
            catch (WebException ex)
            {
                System.Diagnostics.Debug.WriteLine("ERROR:");
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
            }
            host.Close();            

        }
    }
}

The error occurs at this line:

System.Diagnostics.Debug.WriteLine(wc.DownloadString(new Uri(baseAddress, "test")));

WCF returns a 400 bad request error. My understanding of WCF addressing is that the "secure" relative uri should be appended to the base address to make http://localhost:9090/testservice/test, but WCF refuses to respond to anything at that uri, giving me the 400 error instead. What am I missing?