我正在创建一个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();
我的问题是:
如上方的代码所示,服务在处理完后被关闭,而通道则一直保持开放状态。保持服务开启的行为是否好的行为,或者我是否应该在每次调用时打开服务并在结束后关闭它,因为可能会有两个客户端同时调用同一个服务。
请给予建议。