我正在尝试将一些旧的 .NET Remoting IPC 代码移植到 WCF 命名管道,我知道我可能在这里做一些愚蠢的事情......
我有一个使用 ServiceHost/NetNamedPipeBinding 端点的 Windows 服务。
我有一个使用 ChannelFactory/NetNamedPipeBinding 连接到它的 WinForms UI。
无论 Windows Server 是否正在运行,在我CreateChannel
在客户端上运行之后,ChannelFactory 对象的状态都是 Opened。如果服务没有运行,我希望它不是“打开”。直到我尝试调用远程方法时才收到异常。检测连接是否成功的最佳方法是什么?
如果服务实际上正在运行,我的代码可以正常运行,但是当它没有运行时(或连接断开),我似乎无法使客户端状态从已打开。
这是我的服务助手代码:
public class WcfServerHelper
{
public static ServiceHost RemoteServiceHost { get; set; }
public static void Start(Type remotingType)
{
RemoteServiceHost = new ServiceHost(remotingType, new Uri[] {
new Uri("net.pipe://localhost")
});
RemoteServiceHost.AddServiceEndpoint(typeof(ICardPrinterRemoting), new NetNamedPipeBinding(), "LifeMedPrinter");
RemoteServiceHost.Open();
}
public static void Stop()
{
if (RemoteServiceHost != null && RemoteServiceHost.State == CommunicationState.Opened)
{
RemoteServiceHost.Close();
}
}
}
视窗服务:
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WcfServerHelper.Start(typeof(CardPrinterRemoting));
}
protected override void OnStop()
{
WcfServerHelper.Stop();
}
}
客户端助手代码:
public class WcfClientHelper
{
public static ICardPrinterRemoting RemotingObject { get; set; }
public static ChannelFactory<ICardPrinterRemoting> PipeFactory { get; set; }
public static void ConnectIpc()
{
PipeFactory = new ChannelFactory<ICardPrinterRemoting>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/LifeMedPrinter"));
RemotingObject = PipeFactory.CreateChannel();
}
}
Windows UI(服务客户端)代码:
private void btnConnect_Click(object sender, EventArgs e)
{
WcfClientHelper.ConnectIpc();
// The event handlers never get called, even if a successfully connection is made and then the Windows Service is stopped
WcfClientHelper.PipeFactory.Closing += PipeFactory_Closing;
WcfClientHelper.PipeFactory.Faulted += PipeFactory_Faulted;
WcfClientHelper.PipeFactory.Closed += PipeFactory_Closed;
if (WcfClientHelper.PipeFactory.State == CommunicationState.Opened)
{
LifeMedPrinterSettings lifeMedPrinterSettings = new LifeMedPrinterSettings();
lifeMedPrinterSettings.PrinterName = "Hello World";
// It always falls into this IF statement, but will blow up on this method call if the service isn't actually running:
WcfClientHelper.RemotingObject.SaveSettings(lifeMedPrinterSettings);
}
else
{
MessageBox.Show("No can-do");
}
}
这是我在服务未运行时调用远程方法时收到的异常:
System.ServiceModel.EndpointNotFoundException was unhandled
HResult=-2146233087
Message=There was no endpoint listening at net.pipe://localhost/LifeMedPrinter that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Source=mscorlib
StackTrace:
Server stack trace:
at System.ServiceModel.Channels.PipeConnectionInitiator.GetPipeName(Uri uri, IPipeTransportFactorySettings transportFactorySettings)
at System.ServiceModel.Channels.NamedPipeConnectionPoolRegistry.NamedPipeConnectionPool.GetPoolKey(EndpointAddress address, Uri via)
at System.ServiceModel.Channels.ConnectionPoolHelper.TakeConnection(TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at LifeMed.CardPrinter.Remoting.Interfaces.ICardPrinterRemoting.SaveSettings(LifeMedPrinterSettings printerSettings)
InnerException: System.IO.PipeException
HResult=-2146232800
Message=The pipe endpoint 'net.pipe://localhost/LifeMedPrinter' could not be found on your local machine.
ErrorCode=-2146232800
InnerException:
谢谢!