TypeLoadException from log4net LogManager.GetLogger - c#

I'm attempting to use log4net to provide logging in a Windows CE/Windows Embedded project. However, when debugging the project I get the following exception
System.TypeLoadException occurred
Message="TypeLoadException"
StackTrace:
at log4net.LogManager.GetLogger(Assembly repositoryAssembly, String name)
at log4net.LogManager.GetLogger(Type type)
at Sis.SiLogCE.Broker.StartBroker..cctor()
at Sis.SiLogCE.Broker.EntryPoint.Program.Main()
from the following line:
private static readonly ILog log = LogManager.GetLogger(typeof(StartBroker));
At first, I thought I might have been using the wrong dll/an old dll so I downloaded the most recent version - I am using the log4net.dll from 1.2.12-bin-newkey and found in .net-cp 3.5.
I've looked though other examples and though the documentation and I can see no reason why I'm getting a type load exception here. I'm now pretty much out of ideas as to what couple be causing the exception.
Background information:
Application is to be deployed on Windows CE 6 and Windows Embedded/Windows Mobile 6 and 6/5 devices.
.net 3.5
.netcf
The following block shows loading the config for log4net from an XML file named "log4net.config".
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().
GetModules()[0].FullyQualifiedName) + "log4net.config";
if (File.Exists(path))
{
XmlConfigurator.ConfigureAndWatch(new FileInfo(path));
}
EDITS:
Up until now, I've only tested on Wince 6.0 so I've just had a shot with Windows Mobile 6.5 - it's no different, the same exception is presented.

Are you using the binary release, or have you built from source ? I ask as this page mentions that Compact Framework 1.0 & 2.0 are not supported by the binary release, but are supported if built from the source release.

Related

Crash C# Application and Generate Dumps

I'm trying to generate dumps for my C# application,
I used the FailFast method to "Immediately terminates a process after writing a message to the Windows Application event log, and then includes the message and optional exception information in error reporting to Microsoft."
static void Main(string[] args)
{
string causeOfFailure = "This is a test crash.";
try
{
Environment.FailFast(causeOfFailure);
}
finally
{
Console.WriteLine("This finally block will not be executed.");
}}
I managed to get the error on event viewer, however Dumps are not generated
This is my settings for the dumps in the registry:
I kept default values, so the Dump folder should be %LOCALAPPDATA%\CrashDumps
However, no dumps are generated
I see in your event viewer screenshot that your app uses .NET Framework 2.0. It seems the crash dump only works in .NET Framework 4.0 and above. Try changing the targeted .NET Framework.
In your Solution Explorer, right-click your project and select Properties
In Properties, go to the Application option on the side menu
Locate the Target framework dropdown and select the framework version 4.0 or above.
I tested this on my machine. When targeting .NET 2.0 there is no crash dump. When targeting .NET 4.0 I get a crash dump.

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.

Nodatime throwing exception in UWP app targeting Windows 10 10240

Hi I'm building a Windows 10 app and I'm using NodaTime 2.4.0 to handle some timezone scenarios. Whilst on debug mode it's working fine, this peace of code throws an "Aggregate Exception" with not much detail.
private void SetDateTime()
{
_currentTimeZone = DateTimeZoneProviders.Tzdb.GetSystemDefault();
}
Which yields the exception:
+ [0] {System.IO.FileNotFoundException: The system cannot find the file specified. (Excep_FromHResult 0x80070002)} System.Exception {System.IO.FileNotFoundException}
I'm compiling the app on Release using .net Native an targeting 10240. Any ideas why it's happening? thanks!

Instance of AggregateException causes TargetInvocationException

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.

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"

Categories