C# – Test Client can not connect to WCF Service self-hosted in winforms application

cexception handlingnetwcf

I have a WCF Service self-hosted in a winforms application. 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. 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.

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

Metadata contains a reference that cannot be resolved: 'http://localhost:8001/HelloWorld'.

There was no endpoint listening at http://localhost:8001/HelloWorld 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:8001HTTP GET Error URI: http://localhost:8001/HelloWorld

There was an error downloading '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

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

Best Answer

I'm not sure why, but switching the using(){} to a try{}..catch{} allows this code to function properly. The WCF Test Client can successfully add the service and I can browse to the running service via: http://localhost:8001/HelloWorld

Related Topic