Can't access most standalone Windows 7 WCF application in non-domain

Trying to run a bootable application on my Win 7 system with little success. The application starts, but I cannot access it from the WCF Test Client or by adding a link in VS. I've read that there seem to be 1000 posts of similar problems, but none of the solutions seem to work.

I did this:

netsh http add urlacl url=http://+:9090/hello user=LocalPC\UserName

      

And then this:

netsh http add iplisten ipaddress=0.0.0.0:9090

      

Here's the code for

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
        Uri baseAddress = new Uri("http://localhost:9090/hello");

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

            // Add MEX endpoint
            host.AddServiceEndpoint(
              ServiceMetadataBehavior.MexContractName,
              MetadataExchangeBindings.CreateMexHttpBinding(),
              "mex");

            // Add application endpoint
            host.AddServiceEndpoint(typeof(IHelloWorldService), new WSHttpBinding(), "");                

            // 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.
            try
            {
                host.Open();
            }
            catch (Exception excep)
            {
                string s = excep.Message;
            }
        }
    }

      

When I try to access the WCF test client I get:

Error: Unable to get metadata from http: // localhost: 9090 / hello If this is a Windows (R) Communication Foundation service that you will access, make sure you enable metadata publishing to the specified address. To help publish metadata, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455 .
WSRI metadata exchange URI: http: // localhost: 9090 / hello The
metadata contains a link that cannot be resolved: "http: // localhost: 9090 / hello".
There was no endpoint listening on http: // localhost: 9090 / hellothat the message can receive. This is often caused by a bad address or SOAP. See InnerException, if present, for more details.
Failed to connect to remote server. The connection could not be completed. because the target machine actively abandoned it 127.0.0.1:9090
HTTP GET Error URI: http: // localhost: 9090 / hello An error occurred while loading "http: // localhost: 9090 / hello". Failed to connect to the remote server. The connection could not be made because the target machine actively refused it 127.0.0.1:9090

When I try to add a link to the service, I get:

Error loading "http: // localhost: 9090 / hello".
Unable to connect to remote server
No connection could be made because the target computer actively refused it
127.0.0.1:9090 The
metadata contains a link that cannot be resolved: "http: // localhost: 9090 / hello".
There were no listening endpoints at http: // localhost: 9090 / hello that could accept the message. This is often caused by an invalid address or SOAP action. See InnerException, if present, for more details.
Unable to connect to remote server
No connection could be made because the target computer actively abandoned it 127.0.0.1:9090
If a service is defined in the current solution, try building the solution and adding the service reference again.

+3


source to share


1 answer


The problem is that you are letting the ServiceHost go out of scope right away.

The using statement is used for cleanup convenience when that block of code goes out of scope, but you have nothing to prevent it. So essentially you open a connection, but then it is placed almost instantly ... which closes the connection.



As long as you don't run into permission issues, this approach should work for you. That being said, this is just a demo version. In fact, you probably don't want your WCF service to bind directly to your form, but defined at the application level.

public partial class WcfHost : Form
{
    private ServiceHost _svcHost;
    private Uri _svcAddress = new Uri("http://localhost:9001/hello");

    public WcfHost()
    {
        _svcHost = new ServiceHost(typeof(HelloWorldService), _svcAddress);

        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        _svcHost.Description.Behaviors.Add(smb);

        InitializeComponent();

        FormClosing += WcfHost_FormClosing;
    }

    private void WcfHost_Load(object sender, EventArgs e)
    {
        try
        {
            _svcHost.Open(TimeSpan.FromSeconds(10));
            lblStatus.Text = _svcHost.State.ToString();
        }
        catch(Exception ex)
        {
            lblStatus.Text = ex.Message;
        }            
    }

    void WcfHost_FormClosing(object sender, FormClosingEventArgs e)
    {
        _svcHost.Close();

        lblStatus.Text = _svcHost.State.ToString();
    }
}

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

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

      

+2


source







All Articles