Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast COM object to interface type 'WUApiLib.UpdateSession c#

I am using WUApiLib.dll, and writing a program to detect which updates can be downloaded and installed.

    Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", "10.81.4.213");
    UpdateSession session = (UpdateSession)Activator.CreateInstance(t);

    ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0");
    foreach (IUpdate x in SearchResults.Updates)
    {
        Console.WriteLine(x.Title);
    }

most of the time it works perfectly, but in some cases I am getting an error in the casting to UpdateSession:

UpdateSession session = (UpdateSession)Activator.CreateInstance(t);

with the following error:

{"Unable to cast COM object of type 'System.__ComObject' to interface type 'WUApiLib.UpdateSession'. 
This operation failed because the QueryInterface call on the COM component for the interface 
with IID '{918EFD1E-B5D8-4C90-8540-AEB9BDC56F9D}' failed due to the following error: 
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)."}

I dont have firewall configured, as I saw people say in other topics regarding similar error, but I have no idea why I am getting this error. any idea what is the problem and how I can solve it?

like image 988
daniel the man Avatar asked Feb 19 '17 13:02

daniel the man


1 Answers

The RPC server is unavailable

The sometimes clause in the question is the normal behavior for this kind of error. Tells you that you don't in fact have a configuration problem. It is very low-level, your computer can simply not communicate correctly with the server.

A temporary networking problem.

It is the kind of problem you cannot recover from in your program, somebody needs to fix the network or get the server back online. So all you can do is let the user of your program know that right now your feature is unavailable.

Translating errors is not normally a good idea but this one is gobbledegooky enough to consider showing a more helpful diagnostic. Something opaque like "We're sorry, a temporary networking service interruption prevented us from contacting the server. Please try again later or contact IT staff to expedite the problem." If IT staff gets bugged about it often enough then they'll do something to make the network or server more reliable. Which is what it takes, better hardware, not better software.

like image 197
Hans Passant Avatar answered Oct 20 '22 16:10

Hans Passant