WCF命名管道客户端状态始终打开

本文关键字:状态 客户端 管道 WCF | 更新日期: 2023-09-27 18:04:00

我正在尝试将一些旧的。net Remoting IPC代码移植到WCF Named Pipes,我知道我可能在这里做了一些愚蠢的事情…

我有一个Windows服务使用ServiceHost/NetNamedPipeBinding端点。

我有一个WinForms UI连接到它与ChannelFactory/NetNamedPipeBinding。

无论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();
        }
    }
}
Windows服务:

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 (Service Client)代码:

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: 

谢谢!

WCF命名管道客户端状态始终打开

经过一番研究,我找到了一个答案,但不是我想要的。显然,查看服务是否可用的唯一方法是调用它-例如通过通用Ping方法,然后处理异常(就像我在我的问题中遇到的异常)。

所以我想这就是答案了。

我唯一的另一个问题是:国有财产的目的是什么?我希望State == Opened表示与该服务有一个打开的连接。

不管怎样,我有我的解决办法了。

谢谢!