AlexVallat

Hello,

Is it possible to check for an endpoint being available without throwing and catching an EndpointNotFoundException

I'm using code from here to do a Single Instance application, but it is very irritating to have an exception thrown at every normal startup. Exceptions should be used for exceptional conditions, not the normal operation of code.

The code in question is:

public static bool QueryPriorInstance(string[] args, string channelUri) {
try {
IPriorInstance instance = ChannelFactory<IPriorInstance>.CreateChannel(new NetNamedPipeBinding(), new
EndpointAddress(channelUri));

instance.Signal(args); <- Exception is thrown here
return true;
}
catch (EndpointNotFoundException) {
return false;
}
}


Therefore, the normal operation for the application starting is to try and signal an existing instance, and throw an exception when there isn't one. What I'd like to know is if there is any way of determining whether the endpoint of named pipe is available to connect to in a non-exception throwing way!

Thanks,

Alex


Re: Windows Communication Foundation (Indigo) Checking for a named pipe endpoint without throwing EndpointNotFoundException

Allan-Nielsen

Hi,

I surpose you could use WMI to try query a running instance !

but check our IDesign's InProcFactory (search for it on the site) which does exactly what you want with NetNamePipeBinding, support for singleton (they us a static dictonary btw) etc..

hth Allan





Re: Windows Communication Foundation (Indigo) Checking for a named pipe endpoint without throwing EndpointNotFoundException

JDPeckham

you could put a named mutex in your service and check for the mutex before opening the channel... just one thought.




Re: Windows Communication Foundation (Indigo) Checking for a named pipe endpoint without throwing EndpointNotFoundException

Allan-Nielsen

hi,

btw you could also use a mutex but judging from your sample it looks like you're using one already re: the InstanceMutex!

it assumes you're always using netnamepipebinding of course.

sorry if multiple post occurs on this thread, apparently the posting doesn't seem to get updated.

/Allan





Re: Windows Communication Foundation (Indigo) Checking for a named pipe endpoint without throwing EndpointNotFoundException

AlexVallat

I suppose I'll use a Mutex then, and check that first. InstanceMutex in the code I linked to is not actually a Mutex, I'm not sure why he's called it one. Using a Mutex is just side-stepping the issue, though. There should be some way of determining whether an endpoint exists and can have methods called on it without throwing an exception, as this can be a non-exceptional case.

I had a look at IDesign's InProcFactory, but it did not appear to do anything to resolve this issue, as far as I could tell, it does not even try to detect whether there is already a service running started by another instance (and therefore not in the static dictionary). In fact, if you run two instances of the demo app, it throws an exception about the named pipe being already in use.

Thanks for your help,

Alex




Re: Windows Communication Foundation (Indigo) Checking for a named pipe endpoint without throwing EndpointNotFoundException

Allan-Nielsen

hi,

yeah go with a mutext it's fast and should do the job, sorry about the InProcFactory, its been a while since I checked it out.

a last option would be to query your endpoint using WMI, besides granting the right access to the process/user trying to query the endpoint you could use code like this :

Code Snippet

private bool IsRunning(string listenUri)

{

ConnectionOptions connection = new ConnectionOptions();

connection.Impersonation = ImpersonationLevel.Impersonate;

connection.Authentication = AuthenticationLevel.Default;

ManagementScope scope = new ManagementScope(@"\\machinename\root\ServiceModel", connection);

scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM Endpoint");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject m in queryCollection)

{

if (m["ListenUri"].ToString() == listenUri)

return true;

}

return false;

}

bool IsConnected = IsRunning(@"net.pipe://localhost/YourAppNameGoesHere/SingleInstance");