Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up WCF Self-Hosted REST service?

Tags:

rest

c#

wcf

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?

like image 606
Duck Jones Avatar asked Dec 27 '13 17:12

Duck Jones


People also ask

What is self hosting in WCF?

This is referred to as a self hosting WCF service, the exact meaning of Self Hosted is that it hosts the service in an application that could be a Console Application or Windows Forms and so on. Earlier we saw what a WCF Service is in the . Net environment. We can host a WCF service in IIS and a Windows service also.

Is WCF RESTful or SOAP?

By default, Windows Communication Foundation (WCF) makes endpoints available only to SOAP clients.


2 Answers

here you go .. a blog written by me.

Self Hosted RestService

like image 199
shujaat siddiqui Avatar answered Sep 23 '22 19:09

shujaat siddiqui


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.

like image 39
Darin Dimitrov Avatar answered Sep 24 '22 19:09

Darin Dimitrov