InternetExplorer COMException: System.Runtime.InteropServices.COMexception: The RPC server is unavailable - c#

I'm very new to C# so please excuse me if it's a silly question.
I created a console application that uses the "InternetExplorer" from SHDocVw. This application goes to a website and performs some operations. It works just fine on my computer, but when I try to publish it I just can't to get it to work on my colleagues computers. These other computers don't have dotnet core installed.
So if I publish the app as framework-dependant I get a hostfxr.dll missing library error and it obviously doesn't work at all.
I thought I could fix this by publishing the app as self-dependant. This didn't help either. I always need to paste the "Interop.MSHTML.dll" and "Interop.SHDocVw.dll" manually. When I do, the app at least starts. Internet explorer shows up, the "Navigate" command works. But as soon as I try to hide the window, or try to work with elements in the page, it just crashes.
There are two types of errors I get:
Unhandled Exception: System.Runtime.InteropServices.COMexception: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
at SHDocVw.IWebBrowser2.set_Visible(Boolean pBool)
at MyWebApp.IEDriver.Visibility(Boolean isVisible)
at MyWebApp.Program.Main(String[] args)
Other times I get a very similar error, but it's even mentions my own user files, even when running on a different machine. Is that normal?
Unhandled exception. System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (0x800706BA)
at SHDocVw.InternetExplorerClass.get_ReadyState()
at IEAutomation.IEDriver.WaitForComplete() in C:\Users\St3ve\source\repos\MyWebApp\MyWebApp\IEDriver.cs:line 552
at TestWeb.Program.Main(String[] args) in C:\Users\St3ve\source\repos\MyWebApp\MyWebApp\Program.cs:line 82
I tried to to google the errors, but can't get it work, I would be really grateful for any hints or help.

The crux of this approach is to make sure we are accessing the right InternetExplorer object that is associated with our process
private InternetExplorer _IE;
public Process m_Proc = Process.Start(#"C:\Program Files\Internet Explorer\iexplore.exe", "-nomerge www.google.com");
public IEDriver()
{
Thread.Sleep(5000);
_IE = null;
ShellWindows m_IEFoundBrowsers = new ShellWindows();
foreach (InternetExplorer Browser in m_IEFoundBrowsers)
{
if (Browser.HWND == (int)m_Proc.MainWindowHandle)
{
_IE = Browser;
break;
}
}
Then in your methods like WaitForCompleteNew(), Navigate(), etc where ever you attempt to access InternetExplorer's proprties or methods, you can use object _IE.
_IE.Visible = false;
document = ((HTMLDocument)_IE.Document)

Related

Can't access ServiceController (InvalidOperationException) when trying to read on remote/another computer

I have a local service running on my computer and trying to get other computers to be able to read the status of my service (whether it's running, stopped, etc.) However, I am unable to as I get an InvalidOperationException error, saying that I am unable to open Service Control Manager. Locally, I am able to, but on another remote computer I am unable to. The ServiceController (cs) object just returns an object with properties that all have the InvalidOperationException error.
I've tried closing down all the firewalls on the other computers, tried running Visual Studio on Administrator privileges, but nothing seems to be working. I've noticed that others suggested hard coding your admin credentials and using WindowsIdentity and Impersonation but that wouldn't work for my project (as it wouldn't be a viable solution at my workplace - wouldn't make sense with the business logic as don't want to give clients any in-house credentials).
Here's my snippet of code:
public bool CheckServiceStatus()
{
try
{
string machineName = pubSubConfig.MachineName;
string serviceName = pubSubConfig.ServiceName;
System.ServiceProcess.ServiceController cs = new System.ServiceProcess.ServiceController(serviceName, machineName);
if (cs != null && cs.ServiceName == serviceName && cs.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
return true;
}
}
catch(Exception ex)
{
Trace.TraceError("Unable to check service status: /r/n {0}", ex.Message);
}
return false;
}
The error is this:
System.InvalidOperationException: Cannot open Service Control Manager on computer '___'.
This operation might require other privileges. ---> System.ComponentModel.Win32Exception: Access is denied
Does anyone know any workarounds as to how I can get other computers running my C# program to be able to read the ServiceController object?
Thanks!
So as I said in the comments, I was not able to get around the SCM (Service Control Manager) due to admin privileges when accessing another remote computer (makes sense if you think about security). However, I did find another solution that was more or less a workaround. I'll post the solution here in case anyone finds it helpful.
So to check the status of the Windows Service (like if it's running or not), I added an additional WCF service that is hosted in the Windows Service. So now the service can expose a method that literally just returns true.
Essentially the thought-process around it was that if the WCF service is accessible then that means the Window Service is running, and thus will always return true. If the Windows service is down, the WCF service will also be down and thus making that method not available. You wouldn't get anything to return, so you would know that the service is down and not running.
Hope that helps someone! I know it's not really a direct solution to the problem I had originally asked, but it was a workaround, indirect solution.

Autodesk.Inventor.Interop.dll on WinServer 64-bit 2012 R2 - interface non registred

i'm trying to develop a very simple web application based on the Autodesk Inventor engine.
I'm developing on Win7 64-bit with Visual Studio 2010 and Inventor 2015 and everything it's working perfectly on debugging but when i publish on the web server i get the error:
HRESULT: 0x800401F3 (CO_E_CLASSSTRING)
and the message
interface string not valid
The C# code line where i receive the error is:
Inventor.Application _invApp = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
The full code for my test is the following:
using Inventor;
using System.Runtime.InteropServices;
namespace web_debug_cs
{
public partial class debug_runinventor : Ssytem.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) { startInventorApplication(); }
private void startInventorApplication()
{
string sDebug = string.Empty;
try
{
Inventor.Application _invetorApp = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
sDebug = "Success!!!";
}
catch (Exception ex) { sDebug = "UNSUCCESS!<br />" + ex.Message; }
lblAnswer.Text = sDebug;
}
}
}
I get this code directly from the Inventor 2015 guide, but (repeat) on the local machine everything it's ok, but not on the server.
I checked the permission (everyone: full control)
I registred manually with regsvr32 (impossible to register) both on system32 and SysWOW64
I registred on the framework 32 and 64 with regasm (registration success!)
I set the web site to work with 32-bit application
but nothing could solve this issue.
I thought about the possibility to import manually the dll with pInvoke, but with no success...
I googled a lot, i tryed to ask to Autodesk with no success.
I started Inventor on the Server to verify if it works, and it work perfectly!
Could anyone try to help me?
Thanks in advance for any kind reply!
Emanuele
I would not expect this to work due a simple reason: the webpage is running as a service, and Inventor runs as a user-level application. Imagine the following: the web page is requested by 100 clients/browsers, your app will launch Inventor 100 times. But more than that, the webpage is not on the user-level.
The best chance to work would be: your webpage receives a request (let's say create a .ipt file), create a record that the action must be performed (for instance, a new .txt file with instructions or a database entry), a Windows services detects the request and launch Inventor to process it. In this case you'll have a queue where requests are placed by your webpage, but processed later.
But I must say this is not supported nor allowed on the EULA, please review it.

ASP.NET Web Application - on deploy, System.Speech.dll object not getting set to an instance of an object

I have an ASP.NET web application that uses System.Speech to transform text to a WAV file. It works fine locally but when I deploy it to the server, I get the below error message. This is using Windows Server 2012, ASP.NET 4.5, and IIS 8.5:
Object reference not set to an instance of an object.
System.Speech
at System.Speech.Internal.ObjectTokens.RegistryDataKey..ctor(String fullPath, RegistryDataKey copyKey)
at System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut()
at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer)
at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
at QuinnSDS.handlerTransform.<>c__DisplayClass6.<ProcessRequest>b__1()
The code which is generating this error message runs on the server:
if (context.Request.ContentLength > 0)
{
string line = new StreamReader(context.Request.InputStream).ReadToEnd();
// ********* generate wav file voicing the response *****************
// Using Microsoft voices
// initiate new instance of speech synthesizer
Thread t = new Thread(() =>
{
try
{
// The object creation works fine
System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer();
if (synth != null)
{
// The code breaks at synth.GetInstalledVoices() below. It will break any time I try to do anything with the synth object
foreach (System.Speech.Synthesis.InstalledVoice voice in synth.GetInstalledVoices())
{
System.Speech.Synthesis.VoiceInfo info = voice.VoiceInfo;
string voiceName = info.Name;
ws.WriteLine(voiceName);
}
}
}
catch (Exception e)
{
ws.WriteLine(e.Message);
ws.WriteLine(e.Source);
ws.WriteLine(e.StackTrace);
}
//... code continues...
It does not break when the Speech Synthesis object is created; it breaks whenever I try to use that object in any way.
I'm not sure if it's an access issue but I'm pretty new to ASP.NET and IIS and I can't figure out how to give the web app access to the GAC or if that's even what the problem is. I tried changing the property Local Copy for the System.Speech reference to True in Visual Studio, before I deploy the app, but that hasn't worked. I searched online and while the "object reference not set to an instance of an object" seems fairly common, I cannot find any similar issues where it is because of a .NET framework class library...I have run the text-to-speech code locally on the server and it ran fine. I have not run the entire app locally on the server because the web app requires speech input and there is not a microphone on the server.
Any ideas of anything to try would be most welcome!
What user account is the code running under when executed from ASP.NET? If the Speech API is touching the registry like the call stack suggests, it possibly has different permissions than the account you used to run the code manually.
If you can't just make the application pool for your site run with the same account you log into the machine with, I've had some success using Process Monitor to track down this kind of problem before. Basically, execute the code that fails while Process Monitor is running and look for 'ACCESS DENIED' in the 'Result' column (or anything else that looks suspicious). Quickly switching the application pool to use your standard user account will be the fastest way to rule out security or permission related problems though.

Resolve connection issue when calling a WCF service through windows 8 app

I am trying to call a WCF service via a windows 8.1 app that I have created. But when I call it I get the exception:
One or more errors occurred
Unable to connect to the remote server
There was no endpoint listening at http://theRequestedService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
An attempt was made to access a socket in a way forbidden by its access permissions [theIPAddress:thePortNumber]
I have reviewed the settings of the service and they seem to be okay and have checked to see if I can access it the following ways:
access via browser (successful)
access via console application (successful)
run Fiddler4 and my windows 8 app (successful)
access directly from the app (falied)
NB: I ran fiddler4 to try and get an error message that would lead me to the solution
I thought I may have altered a setting in my project, so created a fresh test project, but I get the same issue.
The exception messages seem self explanatory so why am I able to successfully connect via the other methods but not through the app? and what steps need to be taken to resolve this issue?
MY CONNECTION CODE (if this helps with solving the problem)
class ConnectionManager
{
public static ConnectionStatus IsConnected()
{
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
ConnectionStatus result = (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess) ? ConnectionStatus.NotSet : ConnectionStatus.NoInternet;
if (result == ConnectionStatus.NotSet)
{
CommonFunctionsClient client = new CommonFunctionsClient();
try
{
client.ChannelFactory.Endpoint.Binding.SendTimeout = TimeSpan.FromMilliseconds(3000);
if (client.PingAsync().Result)
result = ConnectionStatus.Connected;
else
result = ConnectionStatus.NoWCF;
}
catch (Exception ex)
{
throw ex;
//result = ConnectionStatus.NoWCF;
}
}
return result;
}
}
public enum ConnectionStatus
{
Connected,
NoWCF,
NoInternet,
NotSet
}
For me to resolve this issue I downloaded HtmlAgilityPack
HtmlAgilityPack found here from nuGet
I guess the error was due to an issue with x-paths.
I admit this is far from a complete answer but it may help someone resolve their issue if they come across the same thing.
Hopefully someone could provide a reason this solution worked, or a better solution to this issue.

Why am I receiving exception from Office's Outlook library?

I have an application that calls
Email hello = new Email(appropriate constructor);
hello.Email_Send();
I'm receiving the exception:
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
from System.Runtime.InteropServices.COMException.
using O = Microsoft.Office.Interop.Outlook;
class Email
{
public void Email_Send()
{
O.Application outlook = new O.Application(); //Exception thrown here.
O.MailItem message = outlook.CreateItem(O.OlItemType.olMailItem);
message.To = Receiver;
message.CC = Sender;
message.Subject = Subject;
message.Body = "This is an automated message sent at " + DateTime.Now.ToString("HH:mm:ss") + " about " + Body_Topic + System.Environment.NewLine + Body_Content ;
message.Send();
}
}
This error has never happened previously, and there has been no change to the code that I know of. http://support.microsoft.com/kb/825118 doesn't seem to fit the my symptoms - My computer doesn't stop responding, etc.
Any help diagnosing the issue would be greatly appreciated!
This error was caused by visual studio being run as admin. Outlook doesn't allow separate users to access the same mailbox (I had the outlook application open on desktop). Even though I have local admin access w/ my domain user, running VS as admin must associate the process to a different user object? Not exactly sure how this works, but... Resolved.
I ran into the same issue, and as previously said: if Visual Studio is running as Administrator then Outlook prevents another instance with a different user. My VS solution is starting several projects, and I need it to run as Administrator, so what I did is run Outlook as administrator while debugging. This solved my problem.
Posting one of the comments as an answer.
When we close the running instance of Outlook it works like a charm.
In my situation I'm running under VMs with special security software. On my developer desktop it worked fine when Outlook was open.
But in my secured VM I had to close outlook in order to get this to work. This is likely to due our security software.
When outlook is left running: Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
When it is not running, it works fine.
The accepted answer is not entirely accurate. Actually, the COM will fail if the calling application does NOT have the same access level as the original process. Both need to be at the same access level.
So, if Outlook was started as Administrator and the program being debugged in Visual Studio is also administrator, it will work.
Vica-versa: if Outlook was NOT started as Administrator, then the program being debugged in Visual Studio will also need to be NON-administrator.
Also, you can get this message if not also included a COMReference to Microsoft.Office.Core.

Categories