在windows服务中设置端口号 [英] Set the port number in windows service

查看:44
本文介绍了在windows服务中设置端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发一个windows服务.当服务启动时,有一个端口.我的问题是我们可以为其分配特定的端口号吗?例如,端口号为55431".

I am going to develop a windows service. When the service starts, there is a port with it. My question is that can we assign a specific port number to it? Example, port number is "55431".

推荐答案

是的.假设您使用 WCF 作为通信层,您只需将绑定/协议配置为作为服务配置的一部分进行侦听.在您的服务的 OnStart() 方法中,您将注册端口.当服务停止时,您将取消注册它.

Yes. Assuming you're using WCF as the communication layer, you would just configure the binding/protocol to listen to as part of the service configuration. In your service's OnStart() method you would register the port. You would un-register it, when the service stops.

完整演练

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();
}

这篇关于在windows服务中设置端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆