views:

383

answers:

3

Hi,

I have a problem related to creating an instance of a WCF ServiceHost withing a set of unit tests and a project that is build within an integration build in TFSBuild. The code used in the unit test is:

[TestMethod]
public void Service_Can_Be_Dynamically_Hosted()
{
    ServiceHost host = new ServiceHost(typeof(DiscoveryService));

    host.Open();
    host.Close();
}

The configuration for the service, although it can be created through code directly, is located in an .config file containing the following details:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TappingBoard.Core.Network.DiscoveryServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="TappingBoard.Core.Network.DiscoveryServiceBehavior"
               name="TappingBoard.Core.Network.Services.DiscoveryService">
        <endpoint address="" binding="wsHttpBinding" contract="TappingBoard.Core.Network.Services.IDiscoveryService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8001/TappingBoard/DiscoveryService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

The unit test is working perfectly in the client machines with both administrator users and non-admin users executing the tests locally. In the case of the TFS Build Server, the user that is launching the Build is called TFSBuild and is member of Domain Users and a local Administrator in the TFS Build Server.

Executing the same unit test in the Build Server launches the following exception:

System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http://+:8001/TappingBoard/DiscoveryService/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> System.Net.HttpListenerException: Access is denied

As the TFS Build Server is using port 80 to expose some reports and webs over IIS, I changed the port to be used by the WCF Service to 8001. It didn't worked before with port 80 either.

Is there any option to have this test run on the TFS Build Server? Should I configure anything extra on my build?

In terms of providing the most useful information, the systems used are:

  • Dev Machines: VS2010TS, Windows 7RTM
  • Server Machines: TFS2010, Windows Server 2008 R2 RTM

Thanks in advance for your time and support.

Bests, Miguel.

+1  A: 

This sounds like an issue with UAC. Even if your TFSBuild account is a local administrator, the process will not have Administrator privileges unless explicitly configured to have it.

I've run into this issue many times on my local box (Vista x64), and it has always been related to lack of Administrator rights. I can't tell you why it works in non-admin mode on your dev boxes, but then again I don't know how Win7 works in this regard.

An alternative to running the process with Administrator privileges is to configure the port in advance using netsh.

Mark Seemann
A: 

The exception happens when the system tries to register the url.

The url is registered with WAS (Windows Activayion Services). Could there be a problem that WAS is not set up.

There may be a more helpfull message in the event log.

Shiraz Bhaiji
+2  A: 

Hiya,

had the same problem a while back.
the solution I use is to manually register the http namespace once. Paul Wheeler has an excellent tool for that: HttpNamespaceManager

so from your code: register "http://localhost:8001/TappingBoard/DiscoveryService/" and give the user which runs the unit-tests full access.

hope this helps!

Roel
+1 I came here to post the same tool. Let us know if it works.
Richard Berg
thanks, this worked perfectly... but now i have to have it into account while creating the setup for the application. I can maybe switch to nettcp to avoid this problem. Running the app as administrator is not an option either.
migs212