【发布时间】:2014-09-19 12:03:41
【问题描述】:
我正在用 C# 开发一个应用程序。我创建了将托管 wcf 服务的 ServiceHost 应用程序。 客户端将通过传递一些参数来调用 ServiceHost.exe。 我已经尝试过以下方式。
static class ServiceHost
{
private static ITest channel = null;
static void Main(string[] args)
{
if (String.Compare(args[0], "dooperation", true) == 0)
{
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.Transport);
binding.ReceiveTimeout = TimeSpan.MaxValue;
EndpointAddress ep = new EndpointAddress(address);
channel = ChannelFactory<ITest>.CreateChannel(binding, ep);
channel.DoOpertion1();
channel.Close() // close service
// Make sure the application runs!
Application.Run();
GC.KeepAlive(m_singleInstance);
}
else if (String.Compare(args[0], "stop", true) == 0)
{
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.Transport);
binding.ReceiveTimeout = TimeSpan.MaxValue;
EndpointAddress ep = new EndpointAddress(address);
channel = ChannelFactory<ITest>.CreateChannel(binding, ep);
channel.DoOpertion2();
channel.Close() // close service
// Exit Appli
Application.Exit();
}
}
}
所以我在这里创建通信通道,调用方法并关闭通道。 但是创建命名管道,然后通信通道和服务通道消耗更多时间,所以我想优化调用,以便它在客户端调用 ServiceHost.exe 时创建 WCF 服务通信通道的单个实例。
有什么方法可以创建单个频道实例吗? 如果我们保持打开namepine/通信通道有什么副作用吗?
【问题讨论】: