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
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With