是否可以创建一个“可浏览”的自托管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上获得结果。
新界面:
[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);
}
当你更新你的答案时,根据你的建议得到它的工作更具体,但我认为我很满意我的解决方案。如果需要的话,我当然可以使这个功能更加实用,并将其集成到我们的控制面板中,但原始数据现在可以完成。谢谢你的帮助。 –