views:

63

answers:

2

I have a WCF service with a simple Gallio unit test that calls the service. However, I am confused by the framework's behavior. When I start visual studio for the first time, and try to run the unit test, I get the following error:

System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://localhost:43671/MyService.svc/MyService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> 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:43671 ErrorCode: 10061

However, if I right click on the Service Reference in the unit test project and do "Update service reference", a popup saying that ASP.NET Development server on port 43671 has been started shows up, and the unit test works fine.

How do I add the server start up code to my unit test so I don't have to "Update service reference" every time I want to run a unit test?

Thanks.

+3  A: 

This happens because your WCF service host is not started when you run visual studio. Once you try to update the service reference Visual Studio automatically starts the WCF service host and after that your services will be accessible.

One way to solve this problem is to host your service(s) in IIS. This will ensure that the services will be accessible even if VS is not running.

Other solution would be to add reference (assembly reference) to your service and instantiate the service class directly. You don't need to have the service running to run unit tests against it. So if you have a service class called SomeServiceClass you could create an instance of this class in your unit tests (SomeServiceClass instance = new SomeServiceClass()) and write your asserts against it (without even hosting the service).

Pavel Nikolov
A: 

You can self host a service using ServiceHost. If you use that in the startup code of your unit tests, the WCF service will be hosted in the unit test.

I'm not sure where the app.config is for the unit tests you are hosting. However, you can always run the service in a separate AppDomain and set the AppDOmainSetup.ConfigurationFile

Justin Dearing