The ManagementOptions.Timeout property is intended for WMI semi-sync operations. In particular, the ConnectionOption.Timeout property is not used to control the timeout for a WMI connection. This is because the underlying WMI COM API doesn't support specifying a timeout value. The timeout value is default to the value DCOM specifies (usually 60secs). You can adjust this value via dcomcnfg.exe.
A nicer workaround for the issue is to first ping the server before attempting to make a WMI connection to it. Here is a possible code:
...
using System.Net;
string hostName = "ServerNameHere";
bool serverExists = false;
try
{
System.Net.IPHostEntry hostEntry = Dns.GetHostByName(hostName);
serverExists = true;
}
catch(System.Net.Sockets.SocketException)
{
serverExists = false;
}
if (serverExists)
{
Console.WriteLine(@"\\{0} machine exists", hostName);
}
else
{
Console.WriteLine(@"\\{0} machine does not exist", hostName);
}
--
Andy Cheung
Microsoft WMI Test Engineer
This posting is provided "As Is" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
"Arkady N." <arkan@iname.com> wrote in message news:8650b20e.0207081021.7b914d09@posting.google.com...
hi all,
i am trying to connect to a machine via WMI and get some info from it.
when i connect to an existing machine, everything goes right. but
when i try to connect to a machine that is not on our network (machine
with such IP does not exist) it takes 30 to 60 seconds to come back
with the error "The RPC server is unavailable." i tried to set ConnectionOptions.Timeout to something low, but this property does not
seem to have any impact on ManagementScope.connect().
so, i would like to know if it is possible to cut down on the time it
takes for me to figure out that machine with given IP does not exists.
thank you for all your responses.
below is a piece of code that i am executing (in C#) :
ManagementScope ms;
ConnectionOptions co;
co = new ConnectionOptions();
co.Username = USERNAME;
co.Password = PASSWD;
co.Timeout = TimeSpan.FromMilliseconds(300);
try
{
ms = new ManagementScope("\\\\10.1.10.110\\root\\cimv2",co);
ms.Connect();
}
catch (COMException COMe)
{
if (COMe.ErrorCode == -2147023174)
// this is where "The RPC server is unavailable." error gets caught
...
...
}
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 399 |
Nodes: | 16 (2 / 14) |
Uptime: | 99:58:26 |
Calls: | 8,363 |
Calls today: | 2 |
Files: | 13,162 |
Messages: | 5,897,823 |