Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 512
user3432348 Avatar asked Mar 27 '14 11:03

user3432348


3 Answers

Start the cmd as Administrator and enter:

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

this worked for me.

like image 84
franconegro Avatar answered Nov 01 '22 11:11

franconegro


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.

like image 22
Taladon Avatar answered Nov 01 '22 12:11

Taladon


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"
like image 9
Vladimir Nikotin Avatar answered Nov 01 '22 11:11

Vladimir Nikotin