2011-09-05 50 views
2

是否可以創建一個「可瀏覽」的自託管WCF服務,即可以在Web瀏覽器中查看和訪問,並且是我的場景(描述如下)的好主意?是否有可能和明智的解決方案來創建一個「可瀏覽」的自託管WCF服務?

我們有一個Windows服務,偶爾需要調用它來檢索一些診斷數據。我的想法是自我託管服務,然後RDC進入服務器,啓動IE,並訪問瀏覽器中的服務(因此我只啓用了本地主機綁定)。我也不知道該怎麼做的一個可以接受的選擇,可能是打開該服務直到互聯網訪問,但將其限制爲具有管理員權限的Windows帳戶,然後我可以編寫一個簡單的程序來調用該服務並獲取數據。這樣做的缺點是我們的防火牆是一個噩夢配置,所以我寧願不必打開另一個端口。

這裏是我到目前爲止的代碼:

WCF合同及其履行情況:

/// <summary> 
/// Windows Communications Foundation Diagnostics Service contract 
/// </summary> 
//[Service Contract] // commented out and space added as current code doesn't work with these attributes in place; they're needed if using WebServiceHost (which didn't help) 
public interface IDiagnosticsService 
{ 
    //[Operation Contract] 
    List<ConnectedModemDiagnosticsClass> EnumerateConnectedModems(); 
} 

/// <summary> 
/// Windows Communications Foundation Diagnostics Service class 
/// </summary> 
public class WCFDiagnosticsService : IDiagnosticsService 
{ 
    public List<ConnectedModemDiagnosticsClass> EnumerateConnectedModems() 
    { 
     return TcpServerSingleton.Instance.EnumerateConnectedModems(); 
    } 
} 

「工廠」 的功能:

public static ServiceHost HttpSelfHostFactory(int port, string serviceNameForUri, Type serviceType, Logging logObject, string hostName = "localhost", bool publishMetadata = true, bool startListening = true) 
    { 
     // Argument checking: 
     if (string.IsNullOrWhiteSpace(serviceNameForUri)) 
      throw new ArgumentException("serviceNameForUri"); 

     serviceNameForUri = serviceNameForUri.Trim(); 

     if (hostName != null) 
      hostName = hostName.Trim().ToLower(); 

     switch (hostName) 
     { 
      case "localhost": 
      case "127.0.0.1": 
       break; 

      case null: 
      case "": 
       throw new ArgumentException("hostName"); 

      default: 
       throw new NotImplementedException("Non localhost bindings not yet implemented. Need to ensure security."); 
     } 

     ServiceHost selfHost = null; 

     try 
     { 
      // Create Uri: 
      Uri baseAddress = new Uri(string.Format("http://{0}:{1}/{2}", hostName, port, serviceNameForUri)); 
      logObject.WriteLogFile("", "Starting WCF server on " + baseAddress); 

      // Create the ServiceHost: 
      selfHost = new ServiceHost(serviceType, baseAddress); 

      // Enable metadata publishing: 
      if (publishMetadata) 
      { 
       ServiceMetadataBehavior smb = new ServiceMetadataBehavior {HttpGetEnabled = true, MetadataExporter = {PolicyVersion = PolicyVersion.Policy15}}; 
       selfHost.Description.Behaviors.Add(smb); 
      } 

      // Start listening: 
      if (startListening) 
       selfHost.Open(); 
     } 
     catch (Exception ex) 
     { 
      logObject.WriteLogFile("WCF startup exception " + ex.Message); 

      if (selfHost != null) 
      { 
       try { selfHost.Close(); } 
       catch { } 
       selfHost = null; 
      } 
     } 

     return selfHost; 
    } 
} 

實例化:

_selfHostDiagnostics = HttpSelfHostFactory(Settings.Default.WCFHttpDiagnosticsPort, "Diagnostics", typeof(WCFDiagnosticsService), FTArmada.Logging); 

更新:現在有這個工作,謝謝,並接受答案。我已轉換爲REST並註釋了我的服務合同界面,現在可以在http://localhost:85/Diagnostics/EnumerateConnectedModems上獲得結果。

這些鏈接幫助我實現它:12

新界面:

[ServiceContract] // commented out and space added as current code doesn't work with these attributes in place; they're needed if using WebServiceHost (which didn't help) 
public interface IDiagnosticsService 
{ 
    [OperationContract] 
    [WebGet] 
    List<ConnectedModemDiagnosticsClass> EnumerateConnectedModems(); 
} 

在工廠方法創建REST端點(?):

if (rest) 
      { 
       selfHost = new WebServiceHost(serviceType, baseAddress); 
       selfHost.AddServiceEndpoint(implementedContract, new WebHttpBinding(), ""); 
       ServiceDebugBehavior stp = selfHost.Description.Behaviors.Find<ServiceDebugBehavior>(); 
       stp.HttpHelpPageEnabled = httpHelpPageEnabled; 
      } 
      else 
      { 
       selfHost = new ServiceHost(serviceType, baseAddress); 
      } 

回答

2

如果您基於webHttpBinding創建WCF REST服務 - 那本來就是「可瀏覽的」。

查看MSDN Developer Center for WCF REST瞭解更多詳情。

通過實例化WebServiceHost而不是更通用的ServiceHost,您基本上可以將您的WCF服務託管爲REST服務。

完成後,您可以通過從瀏覽器發出HTTP請求來「導航」您的服務。所以要

http://yourURL/diagnostics/modems 

可能會顯示所有已安裝的調制解調器的列表,你可以通過一個URL類似手段「導航」,以一個調制解調器:

http://yourURL/diagnostics/modems/4711 

如果你的調制解調器有一些獨特的ID - 然後這個URL可能會顯示狀態頁面或其他內容。

+1

當你更新你的答案時,根據你的建議得到它的工作更具體,但我認爲我很滿意我的解決方案。如果需要的話,我當然可以使這個功能更加實用,並將其集成到我們的控制面板中,但原始數據現在可以完成。謝謝你的幫助。 –

0

爲什麼不舉辦一個網站,並通過使用嵌入式Web服務器,如this一個在windows服務中提供asp.net頁面一個

+0

我需要一些輕量級的東西,因爲這純粹用於診斷,當出現錯誤時,希望很少會永遠不會。 –

相關問題