WCF 服务应该一直保持打开状态吗?
我正在创建一个 wcf 服务,我的 wcf 服务现在托管在控制台应用程序中,如下所示
PersonService = new ServiceHost(typeof(PersonService));
PersonService.AddServiceEndpoint(typeof(IPersonService), binding, "http://localhost:5645/PersonService");
PersonService.Open();
然后我使用 ChannelFactory 类使用 wcf 服务;
EndpointAddress endPoint = new EndpointAddress("http://localhost:5645/PersonService");
ChannelFactory<IPersonService> engineFactory = new ChannelFactory<IPersonService>(binding, endPoint);
IPersonService personChannel = engineFactory.CreateChannel();
然后我可以使用此通道来调用方法,例如
personChannel.GetPersonById("1");
personChannel.Close();
我的问题是:
如上面的代码所示,服务始终打开,而通道在完成工作后关闭。这是保持服务打开的好行为吗?或者我应该打开服务,然后在每次调用时关闭它,考虑到我可能有两个客户端同时调用同一个服务。
请指教。
I am creating a wcf service, my wcf service now is hosted in a console application as shown below
PersonService = new ServiceHost(typeof(PersonService));
PersonService.AddServiceEndpoint(typeof(IPersonService), binding, "http://localhost:5645/PersonService");
PersonService.Open();
Then I am consuming the wcf service using the ChannelFactory class;
EndpointAddress endPoint = new EndpointAddress("http://localhost:5645/PersonService");
ChannelFactory<IPersonService> engineFactory = new ChannelFactory<IPersonService>(binding, endPoint);
IPersonService personChannel = engineFactory.CreateChannel();
Then i can use this channel to call a method such as
personChannel.GetPersonById("1");
personChannel.Close();
My Question is:
As shown in the code above, the service is always opened while the channel is closed after finishing work with it. Is this a good behavior to keep the service opened, or i should open the service and then close it on each call taking into consideration that i might have two clients calling the same service at the same time.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您的服务必须保持“开放”——这是您的行话,但实际上“开放”调用会在端口上放置一个侦听器。服务器必须保持监听,以便知道客户端何时想要连接到它。如果没有这个,客户端将不会与不监听的服务者交谈。
当您的客户端完成并获得它想要的内容时,它可以终止(关闭)连接并消失,从而节省双方的资源。
这就像打电话一样。必须有人坐在电话旁边“听”电话铃声。客户拿起电话,拨打服务热线。服务热线电话响起。那个人(正在“听”的人)拿起电话,“连接”(即对话)开始。
当客户完成后,他/她放下电话,连接就结束了。然而,服务热线的接听人员必须继续“监听”新的铃声。
Well, your service must be kept "open" -- that's your lingo, but in reality the "Open" call puts a listener on the port. The server must keep listening in order to know when a client wants to connect to it. Without this, the client won't be talking to a servier that is not listening.
When you client is finished and got what it wants, then it can terminate (close) the connection and go away, saving resources on both sides.
This is like making a phone call. Somebody has to sit next to the phone to "listen" for it to ring. A client picks up a phone and call the service hotline. The service hotline phone rings. That person (who is "listening") picks up the phone and a "connection" (i.e. conversation) starts.
When the client is finished, he/she drops the phone and the connection is over. However, the person manning the service hotline must continue to "listen" for new rings.
一般来说,它应该保持打开状态,否则会破坏旧的代理。
Generally, it should stay open, since otherwise it would break the old proxies.
WCF 服务消耗的资源很少,运行 50 个 WCF 服务不是问题。但请务必在它们之间共享 TCP 端口(在您的情况下为
5645
号码)。WCF services consumes little resources and having 50 WCF services running is not a problem. But please do share the TCP port between them (the
5645
number in your case).