【问题标题】:Why does System.ServiceModel.Dispatcher needs to be fully qualified to compile为什么 System.ServiceModel.Dispatcher 需要完全合格才能编译
【发布时间】:2012-06-29 09:36:11
【问题描述】:

我正在学习 WCF,并在一个简单的 WCF 示例中找到了 this article

在下面的代码中(来自上面的文章),为什么foreach循环中的System.ServiceModel.Dispatcher.ChannelDispatcher在有using System.ServiceModel;的情况下需要完全限定?虽然ServiceHost 不需要完全限定它才能工作,并且它来自与Dispatcher 相同的命名空间。

如果您在循环中从System.ServiceModel.Dispatcher.ChannelDispatcher 中删除System.ServiceModel,则代码不会编译。

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

【问题讨论】:

    标签: c# wcf .net-4.0 namespaces


    【解决方案1】:

    ServiceHost 是 System.ServiceModel 命名空间上的一个类(您在 using 语句中有); ChannelDispatcher 是 System.ServiceModel.Dispatcher 命名空间中的一个类。如果您在下面添加此 using 语句,您将能够使用 ChannelDispatcher 而无需完全限定。

    using System.ServiceModel.Dispatcher;
    

    【讨论】:

    • 所以因为Dispatcher 不是一个类,它不能有通过另一个命名空间的快捷方式?我想我误解了使用块作为所有事物的快捷方式,而不仅仅是类。这是有道理的,因为在很多情况下,除了 using System; 之外,您不必有任何其他 using 声明。谢谢。
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2013-02-06
    相关资源
    最近更新 更多