2012-03-27 117 views
4

我想在下面託管這個服務運行正常,當我在一個不同的Visual Studio運行時打開一個新的項目,並嘗試添加一個Web服務它找不到任何東西?不在指定的地址或本地機器上的任何地址?下面的代碼似乎只在我用同一個解決方案運行時才起作用。未檢測到Web服務?

namespace Students 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create the address for the service 
      Uri address = new Uri("http://localhost:8001"); 
      // Create the binding for the service 
      WSHttpBinding binding = new WSHttpBinding(); 
      // Create the service object 
      StudentService service = new StudentService(); 
      // Create the host for the service 
      ServiceHost host = new ServiceHost(service, address); 
      // Add the endpoint for the service using the contract, binding and name 
      host.AddServiceEndpoint(typeof(IStudentService), 
            binding, 
            "students"); 

      // Open the host 
      host.Open(); 
      Console.WriteLine("Student service started"); 
      Console.WriteLine("Press return to exit"); 
      Console.ReadLine(); 
      // Close the host 
      host.Close(); 
     } 
    } 


} 

我得到的,當我試圖把它從一個新的項目(獨立於當前的解決方案),添加的錯誤是這樣的:

The HTML document does not contain Web service discovery information. 
Metadata contains a reference that cannot be resolved: 'http://localhost:8001/'. 
There was no endpoint listening at http://localhost:8001/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. 
The remote server returned an error: (404) Not Found. 
If the service is defined in the current solution, try building the solution and adding the service reference again. 

,當我下載了這個案例研究(啓動項目),它從控制檯應用程序託管的任何地方都沒有Web或應用程序配置文件。

另請注意我在嘗試添加Web服務時運行該服務。

+0

所以你打開一個新的VS和努力服務引用添加到'HTTP://本地主機:8001'和它給你一個錯誤?什麼是錯誤? – CodingGorilla 2012-03-27 14:58:52

+0

據我所知,VS不會自動檢測任意的本地服務,只有解決方案的那些部分。您是否在服務參考文本框中輸入了URL?它有用嗎? – 2012-03-27 14:59:16

+2

您是否在嘗試從其他項目添加服務引用時運行服務主機(控制檯應用程序)? – daryal 2012-03-27 14:59:46

回答

3

添加元數據交換行爲ServiceHost的你。

namespace Students 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create the address for the service 
      Uri address = new Uri("http://localhost:8001"); 
      // Create the binding for the service 
      WSHttpBinding binding = new WSHttpBinding(); 
      // Create the service object 
      StudentService service = new StudentService(); 
      // Create the host for the service 
      ServiceHost host = new ServiceHost(service, address); 
      // Add the endpoint for the service using the contract, binding and name 
      host.AddServiceEndpoint(typeof(IStudentService), 
            binding, 
            "students"); 

      // Add metadata exchange 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpGetEnabled = true; 
      host.Description.Behaviors.Add(smb); 

      // Open the host 
      host.Open(); 
      Console.WriteLine("Student service started"); 
      Console.WriteLine("Press return to exit"); 
      Console.ReadLine(); 
      // Close the host 
      host.Close(); 
     } 
    } 
} 

http://wcftutorial.net/WCF-Self-Hosting.aspx