WCF Service in Console app throwing error

2019-08-29 10:20发布

BasicHttp and NetTcp Binding hosted in Console App

I have the below web.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <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:59084/"/>
            <add baseAddress="net.tcp://localhost:59076/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

The Interface

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

Class that extends Interface

 using System.ServiceModel;
    namespace HelloService
    {
        public class HelloService : IHelloService
        {
            public string GetMessage(string Name)
            {
                return "Hello " + Name;
            }
        }
    }

and Console App code for hosting

using System.ServiceModel;

namespace HelloServiceHost
{
    class Program
    {
        static void Main()
        {
            using(ServiceHost host  = new ServiceHost(typeof(HelloService.HelloService)))
            {
                host.Open();
                Console.WriteLine("Host Started");
                Console.ReadLine();
            }
        }
    }
}

I am getting below error when i try to run the Console app

HTTP could not register URL http://+:8080/. Your process does not have
access rights to this namespace (see
http://go.microsoft.com/fwlink/?LinkId=70353 for details).
  • I tried other port number like 53895, thinking port 8080 might be pre occupied. No Luck!!
  • When i browsed for this error, i came to know this problem due to my account being non admin. Now my doubt is WCFTest Client also execued under my account itself. How can it run the similar code and i can't?

    Also, any suggestions to make this work would be appriciated. May be something to do with Webconfig again??

Thanks for your help in advance!!

1条回答
做个烂人
2楼-- · 2019-08-29 11:02

Your code looks alright. I tried it and it works. Try the solution here: HTTP could not register URL http://+:8000/HelloWCF/. Your process does not have access rights to this namespace

It basically tells you to close your Visual Studio IDE and open it by doing a right-click "Run as administrator"

查看更多
登录 后发表回答