question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PublishTrimmed with Oracle.ManagedDataAccess.Core causes FileNotFoundException at runtime

See original GitHub issue

Steps to repro.

  1. mkdir Trimmed && cd Trimmed
  2. dotnet new console
  3. dotnet add package Oracle.ManagedDataAccess.Core --version 2.19.60
  4. Replace Program.cs with the following code.
      using System;
      using Oracle.ManagedDataAccess.Client;
    
      namespace Trimmed
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string connectionString = "connection string for Oracle DB";
    
                  using (OracleConnection con = new OracleConnection(connectionString))
                  {
                      using (OracleCommand cmd = con.CreateCommand())
                      {
                          try
                          {
                              con.Open();
                              cmd.BindByName = true;
                              cmd.CommandText = "select id from test_table where id = :id";
                              OracleParameter id = new OracleParameter("id", "test");
                              cmd.Parameters.Add(id);
    
                              OracleDataReader reader = cmd.ExecuteReader();
                              while (reader.Read())
                              {
                                  Console.WriteLine("id: " + reader.GetString(0));
                              }
    
                              Console.WriteLine();
                              Console.WriteLine("Press 'Enter' to continue");
    
                              reader.Dispose();
                          }
                          catch (Exception ex)
                          {
                              Console.WriteLine(ex);
                          }
                          Console.ReadLine();
                      }
                  }
              }
          }
      }
    
    
  5. dotnet publish -c Release --runtime win-x64 /p:PublishTrimmed=true
  6. .\bin\Release\netcoreapp3.1\win-x64\publish\Trimmed.exe

Error

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeInitializationException: The type initializer for 'OracleInternal.ServiceObjects.OracleConnectionImpl' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Oracle.ManagedDataAccess.Types.TimeStamp' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'System.Runtime.Serialization.Formatters.Binary.Converter' threw an exception.
 ---> System.IO.FileNotFoundException: Could not load file or assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 指定されたファイルが見つかりません。
File name: 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
   at System.Reflection.Assembly.Load(String assemblyString)
   at System.Runtime.Serialization.Formatters.Binary.Converter..cctor()


   --- End of inner exception stack trace ---
   at OracleInternal.Common.OracleTimeZone.GetInstance()
   at Oracle.ManagedDataAccess.Types.TimeStamp..cctor()
   --- End of inner exception stack trace ---
   at Oracle.ManagedDataAccess.Types.TimeStamp.InitializelatestTZversion()
   at OracleInternal.ServiceObjects.OracleConnectionImpl..cctor()
   --- End of inner exception stack trace ---
   at OracleInternal.ServiceObjects.OracleConnectionImpl..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance[T]()
   at OracleInternal.ConnectionPool.PoolManager`3.CreateNewPR(Int32 reqCount, Boolean bForPoolPopulation, ConnectionString csWithDiffOrNewPwd, OracleConnection connRefForCriteria, String instanceName, List`1 switchFailedInstNames)
   at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, OracleConnection connRefForCriteria, String affinityInstanceName, Boolean bForceMatch)
   at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, OracleConnection connRefForCriteria, String affinityInstanceName, Boolean bForceMatch)      
   at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword, OracleConnection connRefForCriteria)
   at Oracle.ManagedDataAccess.Client.OracleConnection.Open()
   at Trimmed.Program.Main(String[] args)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jods4commented, Feb 10, 2021

Good news everyone. After seeing Oracle tweet about 2.19.101 saying “BinaryFormatter issues are resolved”, I gave it a try.

It looks like you can now publish link-trimmed self-contained .net core applications without referencing mscorlib 4.0.0.0 and it works. 🎉

0reactions
jods4commented, Nov 27, 2020

@alexkeh I am not aware of any recommandation from SQL team.

As far as my understanding of trimming goes, I’d say the same guidelines that apply to end-developer would apply to ISV.

  1. There’s a list of things that don’t work in single-file publish, mostly revolving around accessing your assembly base path or file. There’s no “work-around” other than replacing the code with an alternative that doesn’t use these APIs. Since ODP.NET works for me, I conclude you don’t do that (and I don’t see why you would).

  2. The “safe” assembly-trimming mode is based on referenced assemblies. As long as you don’t load an assembly dynamically there’s no issue. If you wanted to do that, I think you’d have to document a <TrimmerRootAssembly> that end-developers must add to their build. BinaryFormatter aside, works for me, so I don’t think you do that (and again I don’t see why you would).

  3. The link-trimming mode is based on static code analysis, to find types and members that are actually used. End-developers will encounter issues if ODP.NET performs some non-statically typed code, e.g. by reflection, runtime emitted code, or using dynamic. This can be done but you’d need to use attributes as described in the blog post above to make the linker aware of the dynamically accessed members. Again, works for me, so the commonly used codepaths don’t seem to be concerned by that.

It looks to me like you’re very close to trimming-friendly. That pesky BinaryFormatter is the main roadblock, at least for my basic usage so far.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Oracle.DataAccess FileNotFoundException
Whenever I try to do my unit test, it throws a FileNotFoundException. I thought I've installed and been using Oracle.DataAccess v4.112.3.0, but ...
Read more >
Problem running dotnet Core Example in VS Code - Oracle
ManagedDataAccess.dll: 'The type initializer for 'OracleInternal. ... FileNotFoundException : Could not load file or assembly 'System.
Read more >
Trim self-contained applications - .NET
Learn how to trim self-contained apps to reduce their size. .NET Core bundles the runtime with an app that is published self-contained and ......
Read more >
Oracle.ManagedDataAccess.Core support for ASP.NET 5
Just created new ASP.NET Core project with .NET 5. Added NuGet package Oracle.ManagedDataAccess.Core (2.19.100).
Read more >
Create a single file for application deployment - .NET
[IMPORTANT] To run a single file app on Windows 7, you must use .NET Runtime 6.0.3 or later. Sample project file. Here's a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found