我正在尝试从我的计算机上自托管一些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();
}
Run Code Online (Sandbox Code Playgroud)
这是服务代码:
[ServiceContract]
public interface ITestClass
{
[WebGet]
[OperationContract]
string TestMethod();
}
public class TestClass : ITestClass
{
public string TestMethod()
{
return "SUCCESS";
}
}
Run Code Online (Sandbox Code Playgroud)
从我本地机器上的浏览器,我可以发出一个简单的get请求并获取
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">SUCCESS</string>
Run Code Online (Sandbox Code Playgroud)
但是,当我输入时,我似乎无法从家庭网络上的任何浏览器ping此服务http://<service machine's IP>:8090/testing/testmethod.
任何人都可以告诉我,我缺少哪些配置,以使服务对我本地网络上的其他计算机可见,而无需DNS服务器?
您可能需要使用以下netsh命令注册端口:
netsh http add urlacl url=http://+:8090/ user=\Everyone
Run Code Online (Sandbox Code Playgroud)
另请确保您已禁用托管此服务的计算机上的防火墙。否则,它可能会阻塞端口 的流量8090。