gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:48 25 4
gpt4 key购买 nike

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

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

我有一个通过 ChannelFactory/NetNamedPipeBinding 连接到它的 WinForms UI。

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

如果服务确实在运行,我的代码工作正常,但当它没有运行(或连接断开)时,我似乎无法使客户端状态从打开状态更改。

这是我的服务助手代码:

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

window 服务:

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:

谢谢!

最佳答案

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

所以我想这就是答案。

我唯一的另一个问题是:国​​家属性(property)的用途是什么?我希望 State == Opened 指示与服务的连接已打开。

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

谢谢!

关于c# - WCF 命名管道客户端状态始终打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com