是否可以创建“可浏览”的自托管 WCF 服务,即可以在 Web 浏览器中查看和访问?
我们有一个 Windows 服务,我需要偶尔调用它来检索一些诊断数据。我的想法是自托管一个服务,然后将 RDC 连接到服务器,启动 IE,然后在浏览器中访问该服务(因此我只启用了本地主机绑定(bind))。一个可接受的替代方案,我也不确定该怎么做,可能是将服务开放到互联网访问,但将其限制为具有管理员权限的 Windows 帐户,然后我可以编写一个简单的程序来调用该服务并获取数据.这样做的缺点是我们的防火墙配置起来很麻烦,所以我宁愿不必打开另一个端口。
到目前为止,这是我的代码:
WCF 契约(Contract)和实现:
/// <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);
最佳答案
如果您创建一个基于 webHttpBinding
的 WCF REST 服务——这本来就是“可浏览的”。
查看 MSDN Developer Center for WCF REST了解更多详情。
您基本上可以通过实例化 WebServiceHost
将 WCF 服务托管为 REST 服务而不是更通用的 ServiceHost
。
完成后,您可以通过从浏览器发出 HTTP 请求来“导航”您的服务。所以去
http://yourURL/diagnostics/modems
可能会显示所有已安装调制解调器的列表,并且您可以通过类似于以下内容的 URL“导航”到单个调制解调器:
http://yourURL/diagnostics/modems/4711
如果你的调制解调器有一些唯一的 ID - 那么这个 URL 可能会显示一个状态页面或其他东西。
关于c# - 是否可以创建 "browsable"的自托管 WCF 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308786/