Instance of AggregateException causes TargetInvocationException - c#

I have a problem that an instance of an AggregateException causes a TargetInvocationException after a couple of accesses to an Icon resource.
I broke down the problem to the following steps to reproduce (.Net 4.0 full or client profile):
Create a new WinForms application (a console application will not work)
Add an arbitrary icon (.ico file) to the resources
Add the following code to the constructor:
new AggregateException();
for (var i = 0; ; ++i)
{
var icon = Resources.Certificate;
}
You have to change the resource name to the name of your resource.
That's all. Yes I know that this sample doesn't make sense. It's just to illustrate the problem. My working code is much more complex and all of this code is needed.
Without creating this excection the application will work forewer. But if this exception is created the access to the resource will fail with a TargetInvocationException. The InnerException told me that the operation has been finished successfully(?!?!) having a two-line stack trace in System.Drawing.Icon (ctor + Initialize).
What can I do to prevent this problem?
EDIT
It seems to be a problem using Windows 7. A binary which fails on Win 7 will run correctly in Win 8.1.

I found the reason for that problem:
The following system configuration is needed to reproduce the issue:
Windows 7 German Edition
Microsoft .Net Framework 4.5.2 installed (yes I know my binary is compiled against .Net 4.0)
KB2901983 installed
Having a machine which only contains the .Net Framework without KB2901983 the program works fine. After installing KB2901983 the program fails for the same binary (no recompilation required).
I tried to uninstall KB2901983 but it doesn't help. If it was once installed the program will fail. I tested it on a clean Windows 7 German Edition.

Related

How to retrieve data from Elipse server with .Net Core? Am I restrained to .NET Framework for using a DLL?

I have an Elipse E3 Studio (build 5.0.434) server with a bunch of tags (running on a x64 windows) and I want to read then from a .NetCore (3.0) console application (same machine). The thing is Elipse works with COM (as far as I know) and .NetCore can't natively handle it. Gotta use some Interoperability Library or something. .netCore3.0 Release Notes at Windows Native Interop
To make the Elipse server work I used a hardkey so the server was running locally.
I have named my tag "A1" and set the value inside Elipse.
To make the access I made a C# program using e3DataAccessLib and referenced it on the .csprj.
The Program.cs is as follows :
using System;
using E3DATAACCESSLib;
namespace ElipseNetCore{
class Program{
static void Main(string[] args){
try{
E3DataAccessManager e3DA = new E3DataAccessManager();
e3DA.Server = "localhost"; //kinda pointless but still
object Value = new object();
object Timestamp = new object();
object Quality = new object();
e3DA.ReadValue("A1.Value", ref Timestamp, ref Quality, ref Value); //ReadValue is a Elipse Server method that takes in a "tag" and place the result in the ref's
Console.WriteLine($"Value: {Value}, Timestamp: {Timestamp} and Quality: {Quality}");
}//end try
catch(Exception ex){
Console.WriteLine("the mother funking error now is :" +ex.ToString());
//regsvr32 C:\Users\lucas.battistella\Documents\Desenvolvimento\ElipseNetCore\ElipseNetCore\obj\Release\netcoreapp2.2\win-x64\ElipseNetCore.dll
}//end catch try
}//end Main
}//end Program
}//end namespace
The Error I get is the following:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {80327130-FFDB-4506-B160-B9F8DB32DFB2} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Other answers point to a x32 on a x64 or vice-versa issue.
However I've already tried making sure everything is running on x64.
Then I tried everything on x32/x86.
Also tried manually registering the .dll with regsvr32 (as show in the commented out line in the first code block and also the E3DATAACESSLib.dll), got and error popup saying "the said dll was loaded but the entry point DllRegisterServer was not located. Verify if the said dll is a DLL or OCX file"
I've been entangled with this problem for a few days now and since I'm new to all this I don't even know if I'm tumbling in the right direction. I would really appreciate any explanation and please excuse my typos.
How do I retrieve data from an Elipse server? Have I missed something?
UPDATE: I have tried that exact same code on Visual Studio running on .Net Framework 4.7.2 and it worked.
Also tried (still on Visual Studio) on .NetCore and got the aforementioned error.
Work Around:
Forget about NetCore and migrate to NetFramework 4.8. Forget about VSCode and keep rolling with VS.
Every time I look back at this problem it intrigues me. The E3DATAACCESSLib was build against x32 and for NetFramework (which mean Windows necessarily). The weird bit is that it ran on my machine targeting x86 (VS and NetFramework 4.8) but not on VSCode and NetCore. I read conflicting information on libraries built for NetFramework working (or not) on Core.
Today I tried running the built working code on a different machine (virtual and remote) and it showed me the exact same error message. And I fixed it by installing the E3 program and restarting the machine, simple as that.
If that ring any bell to you please share the light.

Are there any dependencies for UI Automation?

I have a Windows application which captures the details from screen based on the configuration. I am using UI Automation to capture the details from the screen. Everything works fine on the developer's machine where Visual Studio is installed. When I run the same application on another system where we have only .NET Framework 4.5 installed, it started behaving strangely, and it's not able to detect the child element.
My question is why it works fine on the developer's machine where Visual Studio and .NET Framework are installed. What's the difference? Is there anything we are missing as far as prerequisites? Any dependencies of UI Automation or any library we are missing..?
Thanks in advance - please help me out.
It looks like a known bug in .NET wrapper around native UIAutomationCore.dll (yes, its core is not a .NET). And it's included into WinVista+ (.NET Framework also adds it even to WinXP).
Here is a C# example how to use native COM API (UIAutomationCore.dll) from C#. Just copying the code here:
using System;
using interop.UIAutomationCore;
namespace PrintDesktopUiaElementNameViaCom
{
class PrintDesktopUiaElementNameViaComProgram
{
static void Main(string[] args)
{
// Instantiate the UIA object:
IUIAutomation _automation = new CUIAutomation();
// Get the root element
IUIAutomationElement rootElement = _automation.GetRootElement();
// Get its name
string rootName = rootElement.CurrentName;
Console.WriteLine(
"The root automation element's name should be 'Desktop'.");
Console.WriteLine("The actual value is: '{0}'", rootName);
}
}
}
Yeah at last after doing day's reading, i came to know the solution is that der is no dependency on Visual studio.
This behavior is due to lack of privileges to the application. so to overcome this behavior we have to get signed our application and one more thing its very important thing is place your executable file in Program Files
Reference Links : https://msdn.microsoft.com/en-us/library/windows/desktop/ee671610(v=vs.85).aspx

Unable to run Console Application from Visual Studio: System.AccessViolationException

Description
When launching any Console Application, the code stops running immediately on an AccessViolationException (Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt). More info included in the next section.
Technical Symptoms
The Call Stack only contains external code:
Exception:Thrown: "The message filter indicated that the application is busy. >(Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))" (System.Runtime.InteropServices.COMException)
A System.Runtime.InteropServices.COMException was thrown: "The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"
Time: 12/10/2015 10:59:55 AM
Thread:vshost.NotifyLoad[15344]
I created a new Console Application, containing only Console.WriteLine("Hello world!");
Running the new Hello world app results in the same exception and an identical
call stack.
Background
I suspect this has nothing to do with the issue, as I will explain, but I feel it is important to answer the obvious question, "what were you doing when the issue happend?" The last change I made was adding an extension to my Selenium Driver to hotkey logout from an application:
public static void logout(this IWebDriver Driver)
{
Driver.FindElement(By.TagName("body")).Click();
new Actions(Driver)
.SendKeys(Keys.Control + Keys.Shift + "x")
.Perform();
}
I also made a change in my App.Config file for one of my projects, but reverting this had no impact either.
Removing this code (the Driver extension and/or the App.Config change) does not resolve this issue. Retrieving a previous check-in does not resolve the issue. I am the only person currently working on this solution.
Discoveries
As mentioned, this is happening for an empty Console Application. Windows Form Applications launch fine. Unit Test Projects launch fine as well.
Research
I've spent hours looking into this. It seems like every similar issue I have looked into is pertinent to debugging only or a .NET version. For me, the issue occurs with a Release as well. Additionally, I have been using .NET 4.5 without any issues or changes on that front. I can't find any articles that seem worth posting, but I might be overlooking something.
Visual Studio Info
Microsoft Visual Studio Ultimate 2013
Version 12.0.30501.00 Update 2
.NET Version 4.6.00081 (just noticed it says 4.5 in my project properties, though)
Running as Administrator
Feedback
When Running from devenv.exe /SafeMode, Visual Studio loaded with:
An exception was encountered while constructing the content of this frame. This information is also logged in "C:\Users\UserName\AppData\Roaming\Microsoft\VisualStudio\12.0\ActivityLog.xml".
Exception details:
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at Microsoft.VisualStudio.Shell.Interop.IVsShell5.LoadPackageWithContext(Guid& packageGuid, Int32 reason, Guid& context)
at Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.GetPackage()
at Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.ConstructContent()
Additionally, when trying to run, I get an error message of "Error while trying to run project: Invalid Pointer" (again, VS in devenv.exe /SafeMode - see how to run visual studio without plugin and all third party feature if unfamiliar)
Please let me know if I can provide any additional information.
It may be because when you name the project you can't put spaces, I put them and it gave me a AccessViolationException aswell, try using capitals to separate your words, but only use letters and no spaces to be sure.
I also faced this issue with Visual Studio 2010. More interestingly I had several projects in my solution (Console application, WPF application, Windows Forms application) but it was failing only when, I was setting the project which was of type "Console Application" as start up project. Following change finally helped me nail down the issue: Go to project properties of the console application project -> Go to "Debug" tab -> Go to "Enable Debuggers" section in right pane -> Check the "Enable unmanaged code debugging" check box as shown in the snapshot below. Root cause of why it happened is still not known to me. Only thing which I observed as fishy was that there were lot of windows updates which had got installed on my machine the previous night which mostly constituted of office updates and OS updates (More than a dozen KB articles).

How can I solve a TypeInitialization exception in my app when deployed?

My app runs fine on my machine (of course/famous last words). However, when deploying it to another machine on the network, it won't even start up. I ran my EventLog utility on that machine and it told me this:
Type:Error
Source: .NET Runtime
Time Generated: 06/12/2012 15:35:12
Message: Application: duckbilledPlatypus.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitialization
Exception
Stack:
at duckbilledPlatypus.PlatypusMainForm..ctor()
at duckbilledPlatypus.Program.Main()
So it's something in my main form's constructor that's the problem, apparently...which, I admit, is a little "busy":
InitializeComponent();
foreach (string arg in Environment.GetCommandLineArgs())
{
if (arg == "-DEBUG")
{
InDebugMode = true;
break;
}
}
SuspendLayout();
tsch = new ToolStripControlHost(dateTimePickerScheduleDate);
toolStripPlatypusMain.Items.Add(tsch);
CreateTableLayoutPanelAndChildren();
LimitPlatypusIDTextBoxesToOneChar();
ShareEventsAmongDynamicTextBoxes();
SetAllPlatypusDataControlsReadOnly();
ResumeLayout();
Does any of this look problematic? (it's all pretty standard stuff, except for the ToolStripControlHost).
Now as to the framework version: does the Event Log report the framework version used to create my app, or the framework version installed on the machine trying to run the app? If the former, and the latter does not have that framework installed, that's a problem, right?
So if that's potentially the problem, how do I determine which version of the .NET runtime is installed on that machine?
UPDATE
I don't know why this is, but I was able to see an err msg after running the app on the remote/deployment only when I right-click and select Run As (and ran as myself, as I don't have any "more special" privileges on that machine). When I did so, I got "Unable to find a version of the runtime to run this application."
If I simply 2-click the app, it dies without a whimper (no err msg).
View the
view the %WINDIR%\Microsoft.NET\Framework directory
to determine which versions of .NET are installed.
http://msdn.microsoft.com/en-us/library/y549e41e.aspx
Try running the Fusion Log Viewer on the problematic machine. It will provide details about binding failures.
http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.100).aspx
Make sure you run it as administrator. It silently fails if you do not.
UPDATE
Based on your error "Unable to find a version of the runtime to run this application.", I would suggest you don't have the same version of .NET installed on the box that your application targets. Did you verify the installed versions using the steps above?
Also see
.Net framework - "unable to find a version of the runtime to run this application"

PerformanceCounters on .NET 4.0 & Windows 7

I have a program that works fine on VS2008 and Vista, but I'm trying it on Windows 7 and VS2010 / .NET Framework 4.0 and it's not working. Ultimately the problem is that System.Diagnostics.PerformanceCounterCategory.GetCategories() (and other PerformanceCounterCategory methods) is not working. I'm getting a System.InvalidOperationException with the message "Cannot load Counter Name data because an invalid index '' was read from the registry."
I can reproduce this with the very simple program shown below:
class Program
{
static void Main(string[] args)
{
foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories())
{
Console.WriteLine(pc.CategoryName);
}
}
}
I did make sure I'm running the program as an admin. It doesn't matter if I run it with VS/Debugger attached or not. I don't have another machine with Windows 7 or VS2010 to test it on, so I'm not sure which is complicating things here (or both?). It is Windows 7 x64 and I've tried forcing the app to run in both x32 and x64 but get the same results.
It seems performance counters were corrupted on my system. Although I didn't follow this post exactly, it led me to the solution. Here is what I did:
In an command prompt with administrator/elevate privileges typed the following:
lodctr /?
Useful stuff in there...
Then typed:
lodctr /R
According to the docs from the prior step, this gets windows to rebuild the perf registry strings and info from scratch based on the current registry settings and backup INI files. I have a feeling this is what did the magic. However, next I noticed the .NET performance counters were not there anymore so based on this I typed the following to reload them:
lodctr "C:\Windows\Microsoft.NET\Framework64\v4.0.20506\corperfmonsymbols.ini"
Note that this path is for .NET Framework 4.0 on x64. You can imagine the path for other variations of the framework/platform. I'm guessing you should always load the counters from the highest version of the .NET framework that you have installed, but that is just a guess.
I hope this helps someone else someday!

Categories