How to set up WCF Self-Hosted REST service?

2019-04-23 18:16发布

问题:

I'm trying to self-host some WCF RESTful services from my computer for consumption by machines on my local network. I have no experience with WCF and am largely a newb when it comes to this. I created a very basic, stripped down console app to see if I could get it working.

static void Main(string[] args)
    {
        Uri httpUrl = new Uri("http://localhost:8090/");

        var host = new WebServiceHost(typeof(TestClass), httpUrl);
        var binding = new WebHttpBinding(); // NetTcpBinding();
        host.AddServiceEndpoint(typeof(ITestClass), binding, "testing");
        ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        stp.HttpHelpPageEnabled = false;

        host.Open();

        Console.WriteLine("Commence with the testing!");
        Console.ReadLine();

        host.Close();
    }

Here's the service code:

[ServiceContract]
public interface ITestClass
{
     [WebGet]
     [OperationContract]
    string TestMethod();
}

public class TestClass : ITestClass
{
    public string TestMethod()
    {
        return "SUCCESS";
    }
}

From a browser on my localmachine, I'm able to issue a simple get request and get

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">SUCCESS</string>

However, I don't seem to be able to ping this service from any browsers on my home network when I type in http://<service machine's IP>:8090/testing/testmethod.

Can anyone tell me what configuration I'm missing to enable the service to be visible to other machines on my local network without needing a DNS server?

回答1:

You might need to register the port by using the netsh command:

netsh http add urlacl url=http://+:8090/ user=\Everyone

Also make sure that you have disabled the firewall on the computer hosting this service. Otherwise, chances are that it might be blocking the traffic to port 8090.



回答2:

here you go .. a blog written by me.

Self Hosted RestService



标签: c# wcf rest