I have a windows desktop application written with C# (Windows forms). In the code, there is a part where I use a simple try-catch exception handler. It works as expected in desktops setting, however, when deployed in Citrix it does not work at all. There is an error that I expect to fire there, I catch it and the application keeps running just fine. Anyone seen anything like it?
EDIT:
This is the code that fails. It may not say much since it is just a try catch where I execute code that is related to a specific library.
try
{
if (this.model != null)
{
if (this.model.Context.GetService<Spotfire.Dxp.Application.Document>().ActivePageReference.ActiveVisualReference != (Spotfire.Dxp.Application.Visual)this.model.Visual)
{
this.model.Context.GetService<Spotfire.Dxp.Application.Document>().ActivePageReference.ActiveVisualReference = (Spotfire.Dxp.Application.Visual)this.model.Visual; //In certain conditions this line may fail, so I trap it.
}
}
}
catch (Exception exc)
{
//System.Diagnostics.Debug.WriteLine(exc.Source + " " + exc.Message + "\n" + exc.StackTrace);
}
I added a comment to the line that fails. I trap the error and it works just fine. The thing is that when it is deployed to citrix it fails to trap the error and it actually crashes the application. Thus my inclination to think that it is something related to citrix.
Thanks!
Related
I have written a windows service in .NET 4 that uses the FirebirdSql.Data.FirebirdClient ADO.NET provider v4.5.2.0.
The service depends on capturing events posted by the firebird Db to perform it's tasks.
The code I use to subscribe to the relevant events is:
<!-- language: lang-cs -->
public void SetFirebirdEventListeners()
{
string[] fbEvents = new string[] { "QueueUpdate", "DispensedSupplyUpdate", "QueueInsert", "DispensedSupplyInsert" };
try
{
fbRemoteEvent = new FbRemoteEvent(fbConn);
fbRemoteEvent.AddEvents(fbEvents);
// Add callback to the Firebird events
fbRemoteEvent.RemoteEventCounts += new FbRemoteEventEventHandler(FbEventHandler);
// Queue events
fbRemoteEvent.QueueEvents();
WriteEventToLog();
}
catch (Exception ex)
{
log.Error("Error setting firebird event listeners. {0}Message:{1}Stacktrace:{2}", Environment.NewLine + Environment.NewLine, ex.Message + Environment.NewLine, ex.StackTrace);
throw;
}
}
The connection to firebird stays open as long as the service is running.
This works fine for a period of time (usually hours), however seemingly at random the service simply stops receiving events. No exceptions are thrown.
Has anyone ever encountered this issue with Firebird and found the cause? I can't see anuthing in the documentation. Is there a way of receiving a warning that the service is no longer subscribed to events?
You are likely confronted with a race condition in the way Firebird registers and notifies events. This bug will be fixed in Firebird 2.5.8 and Firebird 3.0.3. See CORE-5521 for details.
I have a confusing problem and I could not any way to workaround it. I have a couple of lines of codes, and at some point, the Send function throws a System.Net.Sockets.SocketException. However, even though try has the catch block, it does not catches the exception and continue with normal flow. When I try to run the case where it needs to throw same exception, catch captures it pretty well.
Things I have tried:
Trying to change VS debugging settings 'Enable Only For My Code', but
it is already disabled.
I tried to both Debug and Release modes.
I tried using general Exception catcher, but none of them worked.
Here is the code, at some point, the connection from the server closes and the c Socket disconnects. It returns the System.Net.Sockets.SocketException, but below catch does not capture it.
try
{
// Some code here . . .
c.Send(clientData);
Print("Started uploading the file " + uploadFilename + ".");
upBox.Text = "";
Print("Finished uploading the file " + uploadFilename + ".");
}
catch (System.Net.Sockets.SocketException)
{
Print("You are disconnected from server!");
}
The reason you're not getting an exception ...
... is that no exception has occurred!
TCP/IP itself won't necessarily "notice" a disconnect ... unless you explicitly try to write to a closed socket.
Definitely check out Beej's Guide to Network Programming:
http://www.beej.us/guide/bgnet/.
See also:
http://stefan.buettcher.org/cs/conn_closed.html,
How to detect a TCP socket disconnection (with C Berkeley socket)
I have a C#/.NET (VS2010) IE add-on which uses Marshal.GetActiveObject() to a running instance of an application (COM object) and then send commands to it via the Invoke() method. In XP it works fine. In W7/Vista, it requires both IE and the target application to be "Run as Administrator" or else it generates the exception:
[Operation unavailable (Exception from HRESULT:0x8000401E3 MK_E_UNAVAILABLE))]
Here's the code:
private void _BtnPlace_onclick(IHTMLEventObje)
{
....
....
object AutoCADApp = null;
try
{
// Does not return the object from the Running Objects Table unless run 'As Administrator'
AutoCADApp = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
}
catch (Exception ex)
{
MessageBox.Show("Unable to locate a running version of AutoCAD on this machine. Please make sure AutoCAD is running.\n\n [" + ex.Message + "]\n", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
object acadDoc = GetProperty(AutoCADApp, "ActiveDocument");
InvokeMethod(acadDoc, "SendCommand", keyin);
}
Any ideas on how to address the security issues to make this add-on run in W7/Vista (asking it to prompt for elevated privileges is ok)?
Maybe a shoot in the dark, but did you read this code project article:
http://www.codeproject.com/KB/vista-security/ElevatedPrivilegesDemand.aspx
A few points to think about:
Use early binding instead of reflection, invoking methods.
Check if some other code needing the 'Running As Administrator' privilege.
Try the .NET SendStringToExecute() instead.
Check if the command you were trying to invoke has some security
concerns.
I'm constantly getting the following exception which is caused by a user initiating a download and it consequently failing (or being cancelled):
Error Message : The remote host closed
the connection. The error code is
0x80072746. Stack Trace : at
System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[]
status, Byte[] header, Int32
keepConnected, Int32 totalBodySize,
Int32 numBodyFragments, IntPtr[]
bodyFragments, Int32[]
bodyFragmentLengths, Int32
doneWithSession, Int32 finalStatus,
Boolean& async) at
System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean
isFinal) at
System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean
finalFlush) at
I've searched all over the internet, and found an interesting article, however there doesn't seem to be a definitive answer as the best way to prevent this filling up the logs.
The user sees no error and there's no actual problem in the app as it occurs only (to my understanding) in situations out of its control (user cancelling download or loss of connection) but there has to be a way to prevent such an exception being reported.
I hate to say it but I'm tempted to check for this exception and empty catch block its ass away - but this makes me feel like a dirty programmer.
So - what is the accepted method of preventing this exception filling up my mailbox?
The error occurs when you try to send a response to the client but they have disconnected. You can verify this by setting a breakpoint on the Response.Redirect or wherever you are sending data to the client, wait for Visual Studio to hit the breakpoint, then cancel the request in IE (using the x in the location bar). This should cause the error to occur.
To capture the error you can use the following:
try
{
Response.Redirect("~/SomePage.aspx");
Response.End();
}
catch (System.Threading.ThreadAbortException)
{
// Do nothing. This will happen normally after the redirect.
}
catch (System.Web.HttpException ex)
{
if (ex.ErrorCode == unchecked((int)0x80070057)) //Error Code = -2147024809
{
// Do nothing. This will happen if browser closes connection.
}
else
{
throw ex;
}
}
Or in C# 6 you can use Exception filters to prevent having to re throw the error:
try
{
Response.Redirect("~/SomePage.aspx");
Response.End();
}
catch (System.Threading.ThreadAbortException)
{
// Do nothing. This will happen normally after the redirect.
}
catch (System.Web.HttpException ex) when (ex.ErrorCode == unchecked((int)0x80070057))
{
// Do nothing. This will happen if browser closes connection.
}
Which is a better debugging experience since it will stop on the statement throwing the exception with the current state and all local variables preserved instead of on the throw inside the catch block.
You cannot prevent a remote Host to close anything.
And in some protocols this is the normal (or at least accepted) way to say goodbye.
So you will have to handle this specific exception.
From a practical perspective, there is nothing wrong with cancelling a download by virtue of a dead computer or a killed web session, therefore catching remote host closed exceptions is perfectly acceptable.
I've inherited a hoary old piece of code (by hoary, I mean warty with lots of undocumented bug fixes than WTF-y) and there's one part that's giving me a bit of trouble. Here's how it connects to the remote registry to get the add/remove programs key:
try
{
remoteKey = RegistryKey.OpenRemoteBaseKey(
RegistryHive.LocalMachine, addr.Value).OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
return 1;
}
catch (IOException e)
{
IOException myexception = e;
//Console.WriteLine("{0}: {1}: {2}",
// e.GetType().Name, e.Message, addr.Value);
return 2;
}
catch (UnauthorizedAccessException e)
{
UnauthorizedAccessException myexception = e;
//Console.WriteLine("{0}: {1}: {2}",
// e.GetType().Name, e.Message, addr.Value);
return 3;
}
catch (System.Security.SecurityException e)
{
System.Security.SecurityException myexception = e;
//Console.WriteLine("{0}: {1}: {2}",
// e.GetType().Name, e.Message, addr.Value);
return 4;
}
Now, I have two problems:
I know why the IOException - if it's a non-Windows machine it'll throw that. The difference between UnauthorizedAccessException and SecurityException I'm not so clear on. Anyone got any ideas?
This entire bit of code was designed before anyone had thought you might not use your local logon for everything. I can't work out how you do authentication for remotely connecting to the registry, anyway, and this code looks like it's only used in one case, when it can't get this information from WMI.
Any help with either would be great.
You probably have to use impersonation to change the credentials of the thread that calls the remote registry methods. See here (linky) for some information on MSDN. Basically, your thread has a security context that is used to make managed and unmanaged calls.
According to MSDN, UnauthorizedAccessException is not thrown by OpenSubKey. So I think it's not needed.
John's pointer to MSDN answered what UnauthorizedAccessException is for - it only appears when you try to access a key remotely, using OpenRemoteBaseKey.
We're a little wary about changing the security context on the computer - I've found a reference here about using WMI (which we're already using for the vast majority of the heavy lifting) to access the registry, so I might try that instead.