This project is read-only.

How can i host the service in unit tests

Topics: Web Api
Nov 18, 2011 at 3:19 PM
Edited Nov 18, 2011 at 3:19 PM


i'm trying to test service call using nunit. I took some code from the sample test projects. But if i try to run the host ( i get an exception.

My test code:


        public void When_calling_version_without_acceptheader_Then_json_is_returned()
            HttpResponseMessage response = null;
            const string serviceBaseUri = "http://localhost:8080/about";
            const string versionUri = "/version";
            using (var host = new HttpServiceHost(typeof(AboutService), serviceBaseUri))
                var client = new HttpClient();
                response = client.Get(serviceBaseUri + versionUri);
The exception on host.Open():
System.ServiceModel.CommunicationObjectFaultedException : Das Kommunikationsobjekt "Microsoft.ApplicationServer.Http.HttpServiceHost" kann nicht zur Kommunikation verwendet werden, weil es sich im Faulted-Status befindet.
bei System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)
bei ...When_calling_version_without_acceptheader_Then_json_is_returned() in AboutService_Tests.cs: line 21.  
Does anybody know what the problem might be?
Nov 21, 2011 at 3:53 PM

I figured out the problem. Visual Studio must run with administrative privileges. But now i've got the problem with our ci builds. Is there a workaround for this. I don't want to run the build agents under an admin account.

Nov 21, 2011 at 9:03 PM

The admin on the CI machine can setup the http namespace reservation. See for details on how to do this.

Alternatively, you could unit test your service implementation code and extensions without actually hosting the service.

Daniel Roth

Nov 22, 2011 at 10:16 AM

Thanks danroth,

setting the namespace reservation via netsh solved it.

Nov 22, 2011 at 11:55 AM

HI raisr

Out of curiosity, why you do you need to actually host the service in your unit test? In the test above I see you are validating whether or not json is returned. Is the service actually doing the work of ensuring json is return if the version header is not set, or are you using message handlers / operation handlers to do this?

Nov 22, 2011 at 12:13 PM
Edited Nov 22, 2011 at 12:13 PM

Hi Glenn,

yes, it would be very strange to host the service in our unit tests. I just put this simple sample together to show what my problem was. Just hosting and doing some simple response check. The main reason why i need the functionality is to host some services in our integration / acceptence tests. Btw, i ensure that json is the default response type by setting the json formatter as the first in the formatter collection.


public class ApiConfiguration : WebApiConfiguration
    public ApiConfiguration(IApiSettings apiSettings)
        EnableHelpPage = apiSettings.EnableHelpPages;
        EnableTestClient = apiSettings.EnableTestClient;

        CreateInstance = OnCreateInstance;
        ReleaseInstance = OnReleaseInstance;
        var jsonFormatter = Formatters.JsonFormatter;
        Formatters.Insert(0, jsonFormatter);

    private object OnCreateInstance(Type type, InstanceContext instanceContext, HttpRequestMessage message)

    private void OnReleaseInstance(InstanceContext instanceContext, object instance)