2012-06-27 33 views
0

我正在學習WCF,並在簡單的WCF示例上找到了this article爲什麼System.ServiceModel.Dispatcher需要完全限定編譯

在下面的代碼中(來自上面的文章),爲什麼當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); 
      } 
     } 
    } 
} 

回答

4

ServiceHost是System.ServiceModel命名空間(在using語句中)的一個類; ChannelDispatcher是System.ServiceModel上的一個類。 調度員命名空間。如果你在下面添加這個使用語句,你將能夠使用ChannelDispatcher而不是完全合格的。

using System.ServiceModel.Dispatcher; 
+0

因爲'Dispatcher'不是一個類,它不能通過另一個命名空間有一個快捷方式?我想我誤解使用塊是所有事物的捷徑,而不僅僅是類。這很有意義,因爲在許多情況下,除了使用System之外,您不必再使用任何其他'using'語句;'謝謝。 – Jim

相關問題