35

I have created my first self-hosted WCF service. I hosted it in a C# console app but it throws an error:

System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http: 8080

When I run Visual Studio 2013 as administrator, then it works well, but not if I don't. So any way to get it done automatically instead of starting VS as an ADMIN?

So far I created a HelloService class library in which I added a WCF service which consists of an interface IHelloService and HelloService.

IHelloService:

namespace HelloService
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        String GetMsg();
    }
}

HelloService:

namespace HelloService
{
    public class HelloService : IHelloService
    {
        public String GetMsg()
        {
            return "Service Accessed";
        }
    }
}

Then I created a C# console app HelloServiceHost which has an app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors >
        <behavior name="MexBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="HelloService.HelloService" 
               behaviorConfiguration="MexBehaviour" >
        <endpoint 
            address="HelloService" 
            binding="basicHttpBinding" 
            contract="HelloService.IHelloService"></endpoint>
        <endpoint 
            address="HelloService" 
            binding="netTcpBinding" 
            contract="HelloService.IHelloService"></endpoint>
        <endpoint 
            address="mex" 
            binding="mexHttpBinding" 
            contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
            <add baseAddress="net.tcp://localhost:8081/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration> 

and program.cs file:

using HelloService;
using System.ServiceModel;

namespace HelloServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using(ServiceHost sh = new ServiceHost(typeof(HelloService.HelloService)))
            {
                sh.Open();
                Console.WriteLine("Host Started @"+ System.DateTime.UtcNow.ToShortDateString());
                sh.Close();
            }
        }
    }
}

I followed a video tutorial exactly but it's not working why ?

I am using VS 2013, .net 4

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3432348
  • 353
  • 1
  • 5
  • 9
  • possible duplicate of [HTTP could not register URL http://+:8000/HelloWCF/. Your process does not have access rights to this namespace](http://stackoverflow.com/questions/8727293/http-could-not-register-url-http-8000-hellowcf-your-process-does-not-have) – CodeCaster Jan 23 '15 at 13:16

4 Answers4

36

Start the cmd as Administrator and enter:

netsh http add urlacl url=http://+:8080/MyUri user=DOMAIN\user

this worked for me.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
franconegro
  • 361
  • 3
  • 3
  • 1
    For bonus points, remove the permissions afterwards with this -- netsh http delete urlacl url=http://+:8080/MyUri – Tom Cerul Nov 20 '19 at 20:42
22

I ran into the same problem on a different project.

The problem is that binding to a tcp port requires administrative privileges. There's a couple ways to deal with this.

  1. Keep an administrative command prompt open. Then you can just run the console app directly.

  2. (As you suggested) run VS as admin. This is absolutely necessary only when debugging your app.

  3. Create an application manifiest file, specifying requestedExecutionLevel level="requireAdministrator". See How do I force my .NET application to run as administrator? for more details.

Community
  • 1
  • 1
Taladon
  • 269
  • 1
  • 6
  • 1
    Binding to a TCP port does not require administrative privileges. Rather, it is to do with the underlying Windows HTTP API, which is managing the TCP server socket for you. – Aaron Dec 18 '15 at 16:56
9

Root path URL (http://+:8080/) permissions are needed in your case:

netsh http add urlacl url=http://+:8080/ user=%USERDOMAIN%\%USERNAME%

show all URL reservations:

netsh http show urlacl | FIND "URL"
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
-1

If you ran into this error while starting the service on Windows Service, you can check the account on your process installer.

[RunInstaller(true)]
public class ProjectInstaller : Installer
{
    private ServiceProcessInstaller process;
    private ServiceInstaller service;

    public ProjectInstaller()
    {
        process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;
        service = new ServiceInstaller();
        service.ServiceName = "WCFWindowsServiceSample";
        Installers.Add(process);
        Installers.Add(service);
    }
}