2011-09-05 49 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

我需要一些轻量级的东西,因为这纯粹用于诊断,当出现错误时,希望很少会永远不会。 –

相关问题