Search code examples
wcfunit-testingwindows-store-appswindows-8.1

WCF Service times out from windows 8 unit test project


When the application is run, WCF service works as expected.
When the exact same code is called from a unit test project, the error below occurs. Is there something special needed for windows 8 test projects to access the WCF service?

Result Message: Test method DataServiceTests.MyTest threw exception:

System.AggregateException: One or more errors occurred. ---> System.ServiceModel.EndpointNotFoundException: Could not connect to net.tcp://localhost:56478/MyService/DataAccessService. The connection attempt lasted for a time span of 00:00:42.0131535. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 127.0.0.1:56478. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 127.0.0.1:56478

Integration test code:

DataAccess service = new DataAccess(
    new Uri(@"net.tcp://127.0.0.1:56478/MyServices/DataAccessService"));
var bob = service.GetData();

Solution

  • Because this is a store app, it runs in a sandbox (AppContainer) and therefore certain things (like WCF) do not work from "unit tests" out of the box. In order to get this to work, I had to add a Loopback Exemption to my development machine for the unit test project.

    Step 1 Open the Package.appxmanifest file in the unit test project that calls the WCF service, and navigate to the Packaging Tab. From there, note the Package family name.

    Package.appxmanifest

    Step 2 Open the Visual Studio Command Prompt/Developer Prompt, as administrator, and paste this in CheckNetIsolation LoopbackExempt -a -n=UnitTestProjectPackageFamilyName, replacing the last bit with the actual package family name.