【问题标题】:WCF service reference cannot be added无法添加 WCF 服务引用
【发布时间】:2014-05-08 19:17:27
【问题描述】:

我创建了 WCF 自托管服务,我将它托管在 C# 控制台应用程序中,它运行良好,但问题是当我将 localhost URL 放入浏览器时,它无法浏览 + 我无法添加它对 Webforms 客户端应用程序的引用,它会抛出错误:

There was an error downloading 'http://localhost:8084/_vti_bin/ListData.svc/$metadata'.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:8084
Metadata contains a reference that cannot be resolved: 'http://localhost:8084/'.
There was no endpoint listening at http://localhost:8084/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
No connection could be made because the target machine actively refused it 127.0.0.1:8084
If the service is defined in the current solution, try building the solution and adding the service reference again.

我以管理员身份在 VS 2013 的单独实例中运行托管,而在另一个实例中运行客户端,但它不起作用,为什么?

代码:

主机申请:

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

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>

    <services>
      <service name="HellloService.HelloService" behaviorConfiguration="MexBehaviour" >
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HellloService.IHelloService"></endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HellloService.IHelloService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8084/"/>
            <add baseAddress="net.tcp://localhost:8085/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors >
        <behavior name="MexBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

【问题讨论】:

  • 任何答案对您有帮助吗?

标签: c# asp.net wcf c#-4.0 webforms


【解决方案1】:

您的配置文件建议服务地址为:

http://localhost:8084/HelloService

尝试该 URL 以获取服务参考。如果还是不行,请尝试在服务启动时打印端点地址。

【讨论】:

    【解决方案2】:

    每当我在控制台中托管服务时,我都会在 sh.Open() 方法之后将 ServiceHost 的所有端点地址转储到控制台:

    foreach (var endpoint in sh.Description.Endpoints)
    {
      Console.WriteLine(endpoint.Address.Uri);
    }
    

    这适用于通过 .config 文件配置的端点,或者如果您在代码中创建它们。

    【讨论】:

      【解决方案3】:

      在您的配置中添加此端点 (App.config)

      <endpoint address="mex"  binding="mexHttpBinding"  contract="IMetadataExchange" />
      

      错误将得到解决。

      【讨论】:

        【解决方案4】:

        您也可以尝试在 Visual Studio 中启动 WCF 服务,然后从“调试”菜单中选择“全部分离”。这应该让服务继续在后台运行,同时打开 Visual Studio 让您添加服务引用。

        【讨论】:

          猜你喜欢
          • 2020-08-20
          • 1970-01-01
          • 2012-02-15
          • 2011-05-17
          • 2013-09-24
          • 1970-01-01
          • 1970-01-01
          • 2012-01-11
          • 1970-01-01
          相关资源
          最近更新 更多