Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running mstest against a WCF service, WcfSvcHost fails to run and tests fail. Tests pass when debugged

Using Visual Studio 2010, I have written a simple WCF service and some integration tests that I want to run against it. I build my proxy for the tests at runtime in code rather than using configuration.

My tests pass in debug but not when run!

FAIL if run - go Test/Run/Tests in current context ( as the WCF Service it calls has not been hosted)

PASS in debug - go Test/Debug/Tests in current context ( as the WCF project has WCF Options/Start WCF Service Host when debugging another project in the same solution)

Is there a way to get WCFServiceHost to start when the tests are run normally?

Thanks, Andy

Test method BulkLoaderIntegrationTests.IntegrationTests.ImportEntries_withGoodPCMs_reportsCreatedOk threw exception: 
    System.ServiceModel.EndpointNotFoundException: Could not connect to net.tcp://localhost:8001/OLELoader. The connection attempt lasted for a time span of 00:00:00.9687686. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8001.  ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:8001
like image 623
AndyM Avatar asked Oct 14 '22 00:10

AndyM


1 Answers

I disabled 'Start WCF Service Host' when debugging another project in the same solution.

I added a static method in [ClassInitialize] to 'self host' the WCF service within the Test context for the duration of the testing.

        [ClassInitialize]
        public static void Init(TestContext t)
        {
            IntegrationTests.InitService();
        }

        [ClassCleanup]
        public static void CleanUp()
        {
            IntegrationTests.host.Close();         
        }

        private static bool ServiceIsStarted = false;
        private static ServiceHost host;
        private static void InitService()
        {           
            if (!ServiceIsStarted)
            {
                // Create the ServiceHost.
                host = new ServiceHost(typeof (OLEImport),
                                           new Uri(IntegrationTestHelper.BaseAddress));

                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                host.Open();
                ServiceIsStarted = true;
            }
        }
like image 101
AndyM Avatar answered Jan 04 '23 07:01

AndyM