.Net Standard Library With Nuget Packages Used in .Net Framework Project

393 Views Asked by At

I'm currently working with two projects: a .NET Standard 2.0 class library and a .NET Framework 4.6.2 console application. In my .NET Standard library, I'm using the System.Diagnostics.EventLog library.

I wrote a method in .NET Standard project:

public static void LogTest()
{
        if (!EventLog.SourceExists(System.AppDomain.CurrentDomain.FriendlyName))
            EventLog.CreateEventSource(System.AppDomain.CurrentDomain.FriendlyName, "Application");
}

However, when I attempt to call the LogTest method from the .NET Framework console application, I encounter an error stating System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Diagnostics.EventLog, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.'

I was under the impression that the .NET Framework project should automatically copy these dependencies from the .NET Standard library, but it doesn't. So I added
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
line to NET Standard 2.0 class library csproj, it copies the file to build directory of Net Standard project, but still .NET Framework does not have System.Diagnostics.EventLog dll in build directory, so I made their build directory the same, but now I am getting:

System.PlatformNotSupportedException: 'EventLog access is not supported on this platform.'

exception. When I investigated, I saw that the System.Diagnostics.EventLog.dll is for .Net Standard 2.0 in build directory. I copied System.Diagnostics.EventLog.dll for .NET Framework 4.6.2 from Nuget directory (C:\Users\XXX.nuget\packages\system.diagnostics.eventlog\8.0.0).

I could not find a document for this problem, and asked Github Copilot, and it said add the nuget package also to the NET Framework project, and it seems it solved the problem, but this is not a good solution, because I have to know every nuget package references in the other projects.

I uploaded an example project here: https://github.com/sahin52/NetStandardInNetFrameworkProjExcample you can build and see the same problems, and ClassLibrary1\ClassLibrary1.csproj has a line with CopyLocalLockFileAssemblies, which is commented out currently, you can test it by removing comment tags also.

How can I solve this problem in a nice way? Thanks in advance.

Edit: This is not only related to this package, I also got the same error for ModifyRegistry: System.PlatformNotSupportedException: Registry is not supported on this platform..

2

There are 2 best solutions below

3
Charles Owen On

Try this instead you can use the .NET Platform Extensions. This allows you to use Windows-specific libraries in a .NET Core project without having to go through .NET Standard.

First you need the Windows Compatibility Pack.

dotnet add package Microsoft.Windows.Compatibility --version 8.0.0

Then you can install the EventLog library.

dotnet add package System.Diagnostics.EventLog --version 8.0.0
0
Sahin On

The problem is that, .Net Standard build of this dll throws this error, but .net Framework version of the same dll does not throw error. So you need to install the same package to .Net Framework library(Even though it is not used in the project).

When you build .Net Framework project, it will copy the .Net Framework version of the same dll. This way you may use this dll instead of .Net Standard version of it. But be sure to build .Net Framework project every time, because nuget packages are copied automatically.