是否可以創建一個「可瀏覽」的自託管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);
}
當你更新你的答案時,根據你的建議得到它的工作更具體,但我認爲我很滿意我的解決方案。如果需要的話,我當然可以使這個功能更加實用,並將其集成到我們的控制面板中,但原始數據現在可以完成。謝謝你的幫助。 –