0

控制台应用程序中托管的 BasicHttp 和 NetTcp 绑定

我有以下 web.config 文件

<?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>

界面

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

扩展接口的类

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

和用于托管的控制台应用程序代码

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();
            }
        }
    }
}

当我尝试运行控制台应用程序时出现以下错误

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).
  • 我尝试了其他端口号,例如 53895,认为端口 8080 可能已被预先占用。没运气!!
  • 当我浏览此错误时,由于我的帐户不是管理员,我才知道这个问题。现在我怀疑 WCFTest Client 也在我的帐户下执行。它如何运行类似的代码而我不能?

    此外,任何使这项工作的建议都会受到重视。可能与Webconfig再次有关?

提前感谢您的帮助!!

4

1 回答 1

1

你的代码看起来不错。我试过了,它有效。在此处尝试解决方案:HTTP 无法注册 URL http://+:8000/HelloWCF/。您的进程无权访问此命名空间

它基本上告诉您关闭 Visual Studio IDE 并通过右键单击“以管理员身份运行”来打开它

于 2014-06-12T14:34:59.277 回答