【发布时间】:2011-11-14 21:17:38
【问题描述】:
我有一个公开 WCF 服务的 Windows 服务。
我还有一个与 WCF 服务对话并发送命令和接收数据的应用程序。
当我从 Visual Studio 运行应用程序时,这一切正常。
但是,当我安装并运行应用程序时,应用程序无法与服务通信。 应用程序运行的批处理文件也不能停止和启动服务。
我尝试使用 netsh 来“保留”URI,但我真的不知道自己在做什么 :-)
你能指出我正确的方向吗?
Windows Server 代码 WCF 服务代码(略):
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WCF_Service" in both code and config file together.
[ServiceContract]
public class InternetGauge_IO
{
[OperationContract]
public void Pause()
{
RunningState.paused = true;
}
[OperationContract]
public void Continue()
{
RunningState.paused = false;
}
[OperationContract]
public bool GetRunningState()
{
return RunningState.paused;
}
....
Windows Server 代码 WCF 创建端点代码:
private void InitializeConsoleComms()
{
try
{
//Prepare comms with the Console application
Type serviceType = typeof(InternetGauge_IO);
host = new ServiceHost(serviceType, new Uri[] { new Uri("http://localhost:8080/") });
// Add behavior for our MEX endpoint
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
// Create basicHttpBinding endpoint at http://localhost:8080/RJB.InternetGauge/
host.AddServiceEndpoint(serviceType, new BasicHttpBinding(), "RJB.InternetGauge");
// Add MEX endpoint at http://localhost:8080/MEX/
host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");
host.Open();
logger.LogEvent("Console comms ready", "Internet Gauge", 4, 1);
}
catch (Exception e)
{
logger.LogEvent("Failed to open Console comms", "Internet Gauge", 1, 1);
logger.LogEvent("Exception : " + e.InnerException + " Stack Trace: " + e.StackTrace + " Message: " + e.Message, "RJB.InternetGauge.WindowsService.Main", 1, 1);
}
}
应用程序只使用生成的代理和方法,例如
private bool GetRunningState()
{
try
{
InternetGauge_IOClient client = new InternetGauge_IOClient();
isRunning = true;
return(client.GetRunningState());
}
catch (Exception)
{
trayIcon.Text = "Internet Gauge Windows Service does not appear to be running.";
trayIcon.Icon = RJB.InternetGauge.Console.Properties.Resources.ServiceStopped;
isPaused = true;
isRunning = false;
return isPaused;
}
}
我试过的 netsh 命令
netsh http add urlacl url=http://+:8080/InternetGauge_IO user=PC-01\richard
有什么想法吗?
谢谢 理查德
【问题讨论】:
-
什么时候调用 InitializeConsoleComms?它应该在您的服务启动时调用,即 OnStart()。它在调试中工作的原因是否可能是因为 Visual Studio 托管您的 WCF 服务而不是 Windows 服务?
标签: c# wcf windows-services permissions