【问题标题】:Is it possible to create a self-hosted WCF service which is "browsable"?是否可以创建“可浏览”的自托管 WCF 服务?
【发布时间】:2011-09-05 13:28:02
【问题描述】:

是否可以创建“可浏览”的自托管 WCF 服务,即可以在 Web 浏览器中查看和访问?

我们有一个 Windows 服务,我需要偶尔调用它来检索一些诊断数据。我的想法是自托管一个服务,然后 RDC 进入服务器,启动 IE,并在浏览器中访问该服务(因此我只启用了 localhost 绑定)。一个可接受的替代方案,我也不知道该怎么做,可能是将服务开放给互联网访问,但将其限制为具有管理员权限的 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);

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    如果您创建基于 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 可能会显示一个状态页面或其他东西。

    【讨论】:

      【解决方案2】:

      为什么不使用嵌入式 Web 服务器(例如 this one)在 Windows 服务中托管一个网站并提供 asp.net 页面

      【讨论】:

      • 我需要一些轻量级的东西,因为这纯粹是为了在出现问题时进行诊断,希望这种情况很少或永远不会发生。
      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      相关资源
      最近更新 更多