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

时间:2013-03-13 20:57:46

标签: c# .net wcf named-pipes

我正在尝试将一些旧的.NET Remoting IPC代码移植到WCF命名管道,我知道我可能在这里做了一些愚蠢的事......

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

我有一个使用ChannelFactory / NetNamedPipeBinding连接到它的WinForms UI。

无论Windows Server是否正在运行,在客户端执行CreateChannel后,ChannelFactory对象状态都将打开。如果服务没有运行,我希望它不是'Opened'。直到我尝试调用远程方法时才收到异常。检测连接是否成功的最佳方法是什么?

如果服务实际正在运行,我的代码工作正常,但是当它没有(或连接被丢弃)时,我似乎无法使客户端状态从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(服务客户端)代码:

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");
    }
}

这是我在服务未运行时调用remote方法时收到的异常:

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: 

谢谢!

1 个答案:

答案 0 :(得分:1)

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

所以我猜这就是答案。

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

无论如何,我有我的解决方案。

谢谢!