【问题标题】:WCF Service in Console app throwing error控制台应用程序中的 WCF 服务抛出错误
【发布时间】:2014-08-01 11:41:51
【问题描述】:

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 有什么关系??

提前感谢您的帮助!

【问题讨论】:

  • 在您的问题中,您正在讲述 web.config,但在控制台应用程序中,您应该有 app.config
  • @Uriil :对不起,伙计,它是 app.config 文件本身。谢谢

标签: wcf binding console-application wcf-hosting


【解决方案1】:

您的代码看起来不错。我试过了,它有效。 在这里尝试解决方案:HTTP could not register URL http://+:8000/HelloWCF/. Your process does not have access rights to this namespace

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

【讨论】:

  • 我无法在管理员模式下运行 VS IDE,我没有管理员权限。我已经接受了答案,只是看看我是否可以解决这个问题。谢谢老哥
  • @Ramu,很抱歉您仍然遇到问题。如果您可以向他们证明这是导致您出现问题的原因,他们可能会轻松获得管理员权限。
  • 是的,伙计。我只是在测试应用程序。实际上我需要在 IIS 中托管它。只是在控制台应用程序中托管它进行测试。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
相关资源
最近更新 更多