我要开发一个windows服务。当服务启动时,就会有一个端口。我的问题是我们可以为其分配一个特定的端口号吗?例如,端口号为“55431”。
是的。假设您使用 WCF 作为通信层,您只需配置要侦听的绑定/协议作为服务配置的一部分。在您的服务中OnStart()
方法您将注册端口。当服务停止时,您可以取消注册它。
完整演练 http://msdn.microsoft.com/en-us/library/ff649818.aspx
protected override void OnStart(string[] args)
{
// Configure a binding on a TCP port
NetTcpBinding binding = new NetTcpBinding();
ServiceHost host = new ServiceHost(typeof(MyService));
string address = "net.tcp://localhost:55431/myservice"
host.AddServiceEndpoint(typeof(IMyService), binding, address);
host.Open();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)