【问题标题】:How to set up WCF Self-Hosted REST service?如何设置 WCF 自托管 REST 服务?
【发布时间】:2013-12-27 17:47:08
【问题描述】:

我正在尝试从我的计算机自托管一些 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";
    }
}

通过本地机器上的浏览器,我可以发出简单的获取请求并获取

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

但是,当我输入 http://&lt;service machine's IP&gt;:8090/testing/testmethod 时,我似乎无法从家庭网络上的任何浏览器 ping 此服务。

谁能告诉我我缺少什么配置才能使该服务对本地网络上的其他计算机可见,而无需 DNS 服务器?

【问题讨论】:

    标签: c# wcf rest


    【解决方案1】:

    给你.. 我写的博客。

    Self Hosted RestService

    【讨论】:

    • 很棒的 Shujaat,非常有帮助
    【解决方案2】:

    您可能需要使用netsh 命令注册端口:

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

    还要确保您已禁用托管此服务的计算机上的防火墙。否则,它可能会阻塞到端口8090 的流量。

    【讨论】:

    • netsh 命令是我需要学习的东西,但事实证明,Windows 防火墙毕竟是问题所在。当我第一次启动该应用程序时,它问我是否要取消阻止它,我做到了。我什至进去验证了它,它显示“mytestapp.svchost.exe”被允许通过,但是一旦我关闭了防火墙,那就是魔法发生的时候!
    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多