我正在学习 WCF 并找到了 this article在一个简单的 WCF 示例中。
在下面的代码中(来自上面的文章),为什么System.ServiceModel.Dispatcher.ChannelDispatcher
在 foreach 循环中需要完全限定时出现 using System.ServiceModel;
?虽然 ServiceHost
不需要完全限定它就可以工作并且它来自与 Dispatcher
相同的命名空间.
如果删除 System.ServiceModel
来自 System.ServiceModel.Dispatcher.ChannelDispatcher
在循环中,代码无法编译。
using System;
using System.ServiceModel;
namespace ConsoleHost
{
class Program
{
static void Main(string[] args)
{
Type serviceType = typeof(EmailService.EmailValidator);
Uri serviceUri = new Uri("http://localhost:8080/");
ServiceHost host = new ServiceHost(serviceType, serviceUri);
host.Open();
foreach (System.ServiceModel.Dispatcher.ChannelDispatcher dispatcher in host.ChannelDispatchers)
{
Console.WriteLine("\t{0}, {1}", dispatcher.Listener.Uri.ToString(), dispatcher.BindingName);
}
}
}
}
最佳答案
ServiceHost
是 System.ServiceModel 命名空间上的一个类(在 using 语句中); ChannelDispatcher
是 System.ServiceModel 上的一个类。 调度员 命名空间。如果您在下面添加此 using 语句,您将能够使用 ChannelDispatcher 而无需完全限定。
using System.ServiceModel.Dispatcher;
关于c# - 为什么System.ServiceModel.Dispatcher需要全限定才能编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11234934/