当前位置: 代码迷 >> Web Service >> WCF宿主的1点疑问
  详细解决方案

WCF宿主的1点疑问

热度:208   发布时间:2016-05-02 02:50:50.0
WCF宿主的一点疑问
C# code
        static void Main(string[] args)        {            WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);            binding.MessageEncoding = WSMessageEncoding.Text;            Uri addressURI = new Uri("http://www.w3.org:8080/Calculator");            ServiceHost host = new ServiceHost(typeof(Calculator));            host.AddServiceEndpoint(typeof(ICalculator), binding, addressURI);            host.Open();            Console.ReadLine();            host.Close();        }


如上的代码段所示,这个宿主里面包含了WCF的ABC,其中的Uri让人费解。很显然,这里的host为WCF提供了一个接入点。host的主要功能,是侦听来自Internet的消息,或者说,是侦听本机某个网卡上的某个端口。在不知道为网卡指定的内部IP时,可以只指定一个端口号,此时,无论是哪个网卡上的数据,都会被程序所拦截。也就是说,这里的Uri中,只需要给定一个端口号足矣。测试结果也确是如此!无论我怎么更改www.w3.org部分,客户端都可以正常访问。
我不懂了,这里的www.w3.org部分是干嘛用的呢? 是为了让host监听指定的某一个网卡,还是因为Uri格式需要而填上的一个摆设呢?有待高手们帮忙释疑!

------解决方案--------------------
真正写程序的时候会用locahost 替代掉那个 www.w3.org:8080, 比如:



C# code
ServiceHost host = new WebServiceHost(typeof(Calculator), new Uri("http://localhost/Calculator"));
  相关解决方案