Windows Store apps: StreamSocket connection to localhost

Posted on July 8, 2013

The problem: running unit tests on a Windows Store app that connects to a port on localhost results in the error “A connection attempted failed because the connection party did not properly respond after a period of time …”.

Connecting to a loopback address ( or localhost) is not supported in a Windows Store application.  Period.

However, connecting to localhost while debugging an application in Visual Studio IS supported.  For example, suppose you have an app that connects to a web service to consume data, and to test it, you want to host the web service on your development machine.  This does work.  How to get it working is a little bit obscure.

To enable connections to localhost, you need to do two things:

  • Add the Internet (Client) network capability to your package manifest Package.appxmanifest:
    Add network capabilities

    Add network capabilities

    This should be a familiar step.  This capability (and possibly Internet (Client & Server) ) is required for all Windows Store apps that use the internet, even outside of development.  More details here.

  • Enable the loopback exemption for the application. This is the obscure part.  There is a list of apps that are allowed to use loopback (but only in development).  There’s a special tool to manage this: CheckNetIsolation.exe.  Open up the Developer Command Prompt for Visual Studio 2012 and enter
    checknetisolation LoopbackExempt -s

    This shows you the list of exempt apps.  My list looks something like this:

    List Loopback Exempted AppContainers
    [1] -----------------------------------------------------------
     Name: 71019b7c-6915-4a32-a9e6-4b87a5b51d02_x1xh075qegwdj
     SID: S-1-15-2-3309481533-1492278452-4137804630-1388503216-2127054748-690736844-819066020
    [2] -----------------------------------------------------------
     Name: kexp90.3fmseattle.kexpartistdiscovery_svgdnat1p2vd6
     SID: S-1-15-2-2012563013-332449103-1712730079-3179316490-2234986268-3338726070-3088550585
    [3] -----------------------------------------------------------
     Name: AppContainer NOT FOUND
     SID: S-1-15-2-3997858894-1722826965-2906774761-292339451-3072000487-3297839464-3180616200
    [4] -----------------------------------------------------------
     Name: AppContainer NOT FOUND
     SID: S-1-15-2-2670002398-4120870820-383953950-3982430728-1889674389-1530876232-793725804

    To add your app to the list, use this command:

    checknetisolation LoopbackExempt -a -n=package-name

    where package-name is your application’s package name.  You can find this on the Packaging tab of the app manifest editor:

    Windows Store app package name

    Windows Store app package name

You can read more about the checknetisolation tool here.

Now you should be able to use StreamSocket to connect to localhost – but only when debugging.

No Replies to "Windows Store apps: StreamSocket connection to localhost"

    Got something to say?

    Some html is OK