WCF in VS2008 – Hosting is required for Ctrl+F5

Here is another interesting feature of VS2008 about which I could not find any documentation about on MSDN. If we go back to the example from my last post, debugging (F5) the WCF Service Client project is going to start WCF Host Service. However running the same WCF Service client program (Ctrl+F5) is going to result in error:

Unhandled Exception: System.ServiceModel.EndpointNotFoundException: Could not connect to https://localhost:8731/Design_Time_Addresses/WcfServiceLibrary/Service1/.

TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8731.

---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:8731

 

I have to admit that the reason for the error was immediately obvious to me. But if I put myself in shoes of someone who is new to VS or web services, I hardly see how she is expected to figure out the next step from this cryptic error message. And because of a general nature of the error it is easy to get off track if follow discussions of this error on MSDN forum or on other sites.

So what is the root cause for the error? The answer is simple – the service is not running. Client attempts to establish connection to the address of the service on the localhost and it fails to do so because service listener is not running. And this is why it is a TCP error, which reminds us that HTTP is built on top of TCP and it relies on TCP to establish connection to the address.

Now that we establish the root cause for the error, how can one fix it? There are several ways to get Ctrl+F5 running:

  • Set service as a startup project and change Start Options
    • In Solution Explorer, right click on the name of the service project (WcfServiceLibrary in our example) and select Set as StartUp Project

    • In Solution Explorer, right click on the name of the service project (WcfServiceLibrary) and select Properties

    • Open Debug Tab in Project properties

    • Change Command line Arguments within Start Options from

      /client:"WcfTestClient.exe"

      To

          /client:"<pathToClient.exe>"

      where pathToClient.exe is the path to the EXE of your client that you have just built. In my case I replaced it with a full path to WcfServiceClient.exe in bin\Debug folder of the project.

    • CONS of this method is that you would have to change project properties again if you would like to run the standard WcfTestClient.exe to test the service later on

  • Run client after starting debugging of the service
    • In this case you need to start debugging the service project (F5) and then from another instance of Visual Studio or from the command line start the client.
  • Host service
    • Hosting a service this is a whole another topic. It is described in relatively well details on MSDN in Hosting Services topic. I will postpone discussion of details for now.

You may choose any way to solve this error you'd like.

Comments

  • Anonymous
    April 06, 2008
    Here's yet another 'Ctrl+F5' option:
  1. In Solution Explorer, highlight the solution (assuming the solution is showing)
  2. Right Click and select 'Set Startup Projects'
  3. Select 'Multiple startup projects'
  4. Change 'Action' for the Client and the Service to 'Start'
  5. Highlight the service class then use up/down on the right side to move the service class to be before the client class
  6. Click OK
  7. Launch app via Ctrl+F5 Nikola - thanks much for taking the time and effort to do this (and your other) posts.
  • Anonymous
    September 03, 2008
    Thanks, Nikola and John H. I'm just starting to learn WCF and couldn't make the first example in the book work! This post helped me make it work. Thanks again.

  • Anonymous
    November 11, 2008
    Hello I have 6 projects in my Solutions. and I have done same as JohnH suggest but not able to solve my problem.