简体   繁体   English

测试客户端无法连接到WinForms应用程序中自托管的WCF服务

[英]Test Client can not connect to WCF Service self-hosted in winforms application

I have a WCF Service self-hosted in a winforms application. 我有一个WCF服务自托管在winforms应用程序中。 I used the following links: 我使用以下链接:

When I use the WCF Test Client and try to add service I get the following error: Failed to add a service. 当我使用WCF测试客户端并尝试添加服务时,出现以下错误:无法添加服务。 Service metadata may not be accessible. 服务元数据可能无法访问。 Make sure your service is running and exposing metadata. 确保您的服务正在运行并公开元数据。

Error Details: 错误详情:

Error: Cannot obtain Metadata from http://localhost:8001/HelloWorld If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. 错误:无法从http:// localhost:8001 / HelloWorld获取元数据。如果这是您有权访问的Windows(R)Communication Foundation服务,请检查是否已在指定地址启用元数据发布。

For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:8001/HelloWorld 有关启用元数据发布的帮助,请参考MSDN文档, 网址http://go.microsoft.com/fwlink/?LinkId=65455。WS-MetadataExchange错误URI: http:// localhost:8001 / HelloWorld

Metadata contains a reference that cannot be resolved: 'http://localhost:8001/HelloWorld'. 元数据包含无法解析的引用:“ http:// localhost:8001 / HelloWorld”。

There was no endpoint listening at http://localhost:8001/HelloWorld that could accept the message. http:// localhost:8001 / HelloWorld上没有侦听终结点的端点可以接受该消息。 This is often caused by an incorrect address or SOAP action. 这通常是由不正确的地址或SOAP操作引起的。 See InnerException, if present, for more details. 有关更多详细信息,请参见InnerException(如果存在)。

Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8001HTTP GET Error URI: http://localhost:8001/HelloWorld 无法连接到远程服务器无法建立连接,因为目标计算机主动拒绝它127.0.0.1:8001HTTP GET错误URI: http:// localhost:8001 / HelloWorld

There was an error downloading 'http://localhost:8001/HelloWorld'. 下载“ http:// localhost:8001 / HelloWorld”时出错。

Unable to connect to the remote server 无法连接到远程服务器

No connection could be made because the target machine actively refused it 127.0.0.1:8001 无法建立连接,因为目标计算机主动拒绝它127.0.0.1:8001

Here is my Code: 这是我的代码:

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

I'm not sure why, but switching the using(){} to a try{}..catch{} allows this code to function properly. 我不确定为什么,但是将using(){}切换为try {} .. catch {}可以使此代码正常运行。 The WCF Test Client can successfully add the service and I can browse to the running service via: http://localhost:8001/HelloWorld WCF测试客户端可以成功添加服务,并且我可以通过以下网址浏览到正在运行的服务: http:// localhost:8001 / HelloWorld

It seems the root cause is down at the bottom: 根本原因似乎在底部:

Unable to connect to the remote server No connection could be made because the target machine actively refused it 127.0.0.1:8001 无法连接到远程服务器无法建立连接,因为目标计算机主动拒绝它127.0.0.1:8001

I would check your event log to see if the host.Open() call is failing (perhaps due to a firewall or something) or walk through it with your debugger then use telnet to see if it is actually listening on 8001. 我会检查您的事件日志,以查看host.Open()调用是否失败(可能是由于防火墙或其他原因),或者与调试器一起进行遍历,然后使用telnet来查看它是否实际上在监听8001。

Here is The Solution for it Try This it will Work 这是它的解决方案尝试它会起作用

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM