本文介绍了检查WCF(namedpipes)主机是否可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我们有一个winform应用程序只能作为singelton执行,如果第二个实例尝试启动此新实例将连接到当前并通过namedpipes传输参数。

We have a winform application that is only to be executed as a singelton, If a second instance try to start this new instance will connect to the current and transmit parameters over namedpipes.

问题是,启动第一个实例时,将尝试连接到现有的主机。如果主机不存在(就像在这种情况下)一样会抛出异常。处理这个异常没有问题,但我们的开发人员经常使用异常中断,这意味着每次启动应用程序时,开发人员将会遇到两个异常(在这种情况下)。每次开始,Thay都必须按F5两次。

The problem is that when starting the first instance there will be a try to connect to existing host. If the host is not existing(like in this case) an exception will be thrown. There is no problem to handle this exception but our developers is often using "Break on Exception" and that means that every time we startup the application the developer will get two(in this case) breaks about exception. Thay will have to hit F5 twice for every start.

有没有办法检查服务是否可用,如果没有抛出异常?

Is there any way to check if the service is available without throw exception if its not?

BestRegards

Edit1:

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, string lpName);

以下代码说:错误152无法将类型System.IntPtr隐式转换为Orbit.Client .Main.Classes.Controllers.MyClientController.SafeFileMappingHandle'

The following code says : Error 152 Cannot implicitly convert type 'System.IntPtr' to 'Orbit.Client.Main.Classes.Controllers.MyClientController.SafeFileMappingHandle'

using (SafeFileMappingHandle fileMappingHandle
                = OpenFileMapping(FILE_MAP_READ, false, sharedMemoryName))
            {


推荐答案

以下:

if (Debugger.IsAttached)
return true;

这将确保在调试期间检查服务的代码永远不会运行。

This will make sure that the code for checking the service is never runned during debugging.

BestRegards

这篇关于检查WCF(namedpipes)主机是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:26