【问题标题】:Test Client can not connect to WCF Service self-hosted in winforms application测试客户端无法连接到 winforms 应用程序中自托管的 WCF 服务
【发布时间】:2012-05-04 22:02:20
【问题描述】:

我有一个在 winforms 应用程序中自托管的 WCF 服务。我使用了以下链接:

当我使用 WCF 测试客户端并尝试添加服务时,我收到以下错误: 添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。

错误详情:

错误:无法从http://localhost:8001/HelloWorld 获取元数据如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。

有关启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange 错误 URI:http://localhost:8001/HelloWorld 的 MSDN 文档

元数据包含无法解析的引用:'http://localhost:8001/HelloWorld'。

http://localhost:8001/HelloWorld 上没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。有关详细信息,请参阅 InnerException(如果存在)。

无法连接到远程服务器 由于目标机器主动拒绝,无法建立连接 127.0.0.1:8001HTTP GET Error URI: http://localhost:8001/HelloWorld

下载“http://localhost:8001/HelloWorld”时出错。

无法连接到远程服务器

无法建立连接,因为目标机器主动拒绝了它 127.0.0.1:8001

这是我的代码:

public Server()
{
    InitializeComponent();

    using (host = new ServiceHost(typeof(HelloWorldService), new Uri("http://localhost:8001/HelloWorld")))
    {
        // Check to see if the service host already has a ServiceMetadataBehavior
        ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
        // If not, add one
        if (smb == null)
            smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        host.Description.Behaviors.Add(smb);

        //You need to add a metadata exchange (mex) endpoint to your service to get metadata.
        host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

        //http
        host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");

        host.Open();
    }
}

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    string SayHello(string name);
}

public class HelloWorldService : IHelloWorldService
{
    public string SayHello(string name)
    {
        return string.Format("Hello, {0}", name);
    }
}

【问题讨论】:

  • @jskiles1 我关闭了 Windows 防火墙。还是不行。

标签: c# .net wcf exception-handling


【解决方案1】:

我不知道为什么,但是将 using(){} 切换为 try{}..catch{} 可以让这段代码正常运行。 WCF 测试客户端可以成功添加服务,我可以通过以下方式浏览到正在运行的服务:http://localhost:8001/HelloWorld

【讨论】:

  • 这是因为使用最终会处理您的服务。在host.Open() 之后添加Console.ReadLIne() 或类似内容以暂停执行,即使使用using 也可以。
  • @WiktorZychla 谢谢,现在说得通了。
【解决方案2】:

似乎根本原因在底部:

无法连接到远程服务器 由于目标机器主动拒绝 127.0.0.1:8001 无法建立连接

我会检查您的事件日志以查看 host.Open() 调用是否失败(可能是由于防火墙或其他原因),或者使用您的调试器遍历它,然后使用 telnet 来查看它是否真的在侦听 8001。

【讨论】:

  • 我运行程序并尝试使用 telnet。 “远程登录 127.0.0.1 8001”。我收到“连接到 127.0.0.1...无法在端口 8001 上打开与主机的连接:连接失败”。我在测试时关闭了 Windows 防火墙。使用调试器单步执行代码时,host.open() 不会引发异常...
  • 你是说windows事件查看器吗?
【解决方案3】:

这是解决方案 试试这个它会工作的

Uri baseAddress = new Uri("http://localhost:8001/HelloWorld");

    // Create the ServiceHost.
    using (serviceHost = new ServiceHost(typeof(HelloWorldService), baseAddress))
    {
        // Enable metadata publishing.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        serviceHost.Description.Behaviors.Add(smb);

        // Open the ServiceHost to start listening for messages. Since
        // no endpoints are explicitly configured, the runtime will create
        // one endpoint per base address for each service contract implemented
        // by the service.
        serviceHost.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");
        serviceHost.Open();

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2021-09-24
    • 1970-01-01
    • 2012-11-29
    • 2013-02-22
    相关资源
    最近更新 更多