如何设置WCF自托管REST服务?

8

我正在尝试在我的电脑上自行托管一些WCF RESTful服务,供本地网络上的机器使用。我对WCF毫无经验,在这方面基本上是个新手。我创建了一个非常基本、简化的控制台应用程序,以查看是否可以使其正常工作。

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

这是服务的代码:

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

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

我可以在本地浏览器上发出简单的get请求并获取到响应。

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

然而,当我在家庭网络中的任何浏览器中输入http://<服务机器的IP>:8090/testing/testmethod时,似乎无法ping通该服务。

有人可以告诉我缺少哪些配置以使得服务对本地网络中的其他计算机可见,而不需要DNS服务器吗?

2个回答

6
这里是我的博客,内容涉及IT技术。以下是需要翻译的内容:

自托管Rest服务


太棒了,Shujaat非常有帮助。 - Fidel

4
您可能需要使用netsh命令注册端口:
netsh http add urlacl url=http://+:8090/ user=\Everyone

另外,请确保您已在托管此服务的计算机上禁用了防火墙。否则,很有可能会阻塞到端口 8090 的流量。

“netsh” 命令是我需要学习的内容,但结果证明问题实际上出在 Windows 防火墙上。当我第一次启动应用程序时,它询问我是否想要解除阻止,我选择了是。我甚至进去验证过,显示“mytestapp.svchost.exe”已被允许通过,但一旦关闭防火墙,魔法就发生了! - Duck Jones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接