Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssemblyFinder throw Exception in .Net5.0(RC2) SingleFile #239

Closed
xljiulang opened this issue Oct 20, 2020 · 18 comments · Fixed by #241
Closed

AssemblyFinder throw Exception in .Net5.0(RC2) SingleFile #239

xljiulang opened this issue Oct 20, 2020 · 18 comments · Fixed by #241

Comments

@xljiulang
Copy link

xljiulang commented Oct 20, 2020

Unhandled exception. System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
at System.Reflection.RuntimeAssembly.get_CodeBase()
at Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetNormalizedCodeBasePath(Assembly assembly)
at Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetDepsJsonPath(Assembly assembly)
at Microsoft.Extensions.DependencyModel.DependencyContextLoader.LoadAssemblyContext(Assembly assembly, IDependencyContextReader reader)
at Microsoft.Extensions.DependencyModel.DependencyContextLoader.Load(Assembly assembly)
at Microsoft.Extensions.DependencyModel.DependencyContext.Load(Assembly assembly)
at Microsoft.Extensions.DependencyModel.DependencyContext.LoadDefault()
at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy1.CreateValue() at System.Lazy1.get_Value()
at Microsoft.Extensions.DependencyModel.DependencyContext.get_Default()
at Serilog.Settings.Configuration.Assemblies.AssemblyFinder.Auto()
at Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, String sectionName, DependencyContext dependencyContext)
at Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, DependencyContext dependencyContext)
at HostApp.Program.<>c.b__1_2(WebHostBuilderContext hosting, LoggerConfiguration logger)
at Serilog.SerilogWebHostBuilderExtensions.<>c__DisplayClass1_0.b__0(WebHostBuilderContext context, IServiceCollection collection)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass11_0.b__0(HostBuilderContext context, IServiceCollection builder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at HostApp.Program.Main(String[] args)

@xljiulang xljiulang changed the title AssemblyFinder AssemblyFinder throw Exception in .Net5.0(RC2) SingleFile Oct 20, 2020
@nblumhardt
Copy link
Member

Thanks for letting us know 👍

@BalintBanyasz
Copy link

This seems to be related: dotnet/runtime#41265

@nblumhardt Do you think that it can be fixed in Serilog.Settings.Configuration? Currently this is the only thing preventing me from upgrading my SingleFile WinForms App from .NET Core 3.1 to .NET 5.0. Thanks!

@nblumhardt
Copy link
Member

Hi @BalintBanyasz - thanks for the note. Yes, I'd guess there is some way to detect this situation and avoid a crash, here. I am short of time to investigate at the moment; if anyone encountering this has time to debug and propose a fix, that would be welcome.

@tsimbalar
Copy link
Member

In the meantime, you can maybe provide your own implementation of DependencyContext in the call to ReadFrom.Configuration(...) as a workaround ?

@murugaratham
Copy link

tried implementing

var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);
...
.ReadFrom.Configuration(configuration, "Serilog", dependencyContext: functionDependencyContext)

but still getting hit with

Unhandled exception. System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.RuntimeAssembly.get_CodeBase()
   at Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetNormalizedCodeBasePath(Assembly assembly)
   at Microsoft.Extensions.DependencyModel.DependencyContextLoader.GetDepsJsonPath(Assembly assembly)
   at Microsoft.Extensions.DependencyModel.DependencyContextLoader.LoadAssemblyContext(Assembly assembly, IDependencyContextReader reader)
   at Microsoft.Extensions.DependencyModel.DependencyContextLoader.Load(Assembly assembly)
   at Microsoft.Extensions.DependencyModel.DependencyContext.Load(Assembly assembly)
   at xxxx.Api.Program.CreateSerilogLogger(IConfiguration configuration)
   at xxxx.Api.Program.Main(String[] args)

@skomis-mm
Copy link
Contributor

skomis-mm commented Nov 17, 2020

Hi guys, the current workaround for Serilog.Settings.Configuraton 3.1.0 is to use

.ReadFrom.Configuration(configuration, ConfigurationAssemblySource.AlwaysScanDllFiles)

and place all external configuration assemblies to the Using section:

"Using": [ "Serilog.Sinks.Console", "Serilog.Enrichers.Thread", ... etc ],

I've added NotSupportedException handling in the PR and updated the documentation.

@murugaratham
Copy link

any workaround for .net 5?

@skomis-mm
Copy link
Contributor

@murugaratham sorry, I meant Serilog.Settings.Configuration 3.1.0

@murugaratham
Copy link

@skomis-mm yea, i am on .net 5 and i've tried .ReadFrom.Configuration(configuration, ConfigurationAssemblySource.AlwaysScanDllFiles) and also specifying using

"Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Sinks.File"
    ]
...

but i still get same error

@skomis-mm
Copy link
Contributor

@murugaratham , interesting. Do you have any DependencyContext usage left in the code?

at Microsoft.Extensions.DependencyModel.DependencyContext.Load(Assembly assembly)
at xxxx.Api.Program.CreateSerilogLogger(IConfiguration configuration)

Are the any stack frames between these 2 lines?

@murugaratham
Copy link

The stack trace pasted was pretty much it

@skomis-mm
Copy link
Contributor

var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);
...
.ReadFrom.Configuration(configuration, "Serilog", dependencyContext: functionDependencyContext)

Is the DependencyContext.Load called from CreateSerilogLogger(..) ?

@murugaratham
Copy link

Yup, here's a screenshot of the source

Screenshot 2020-11-18 at 9 21 04 AM

Here's the log's after publishing as SingleFile & PublishTrimmed
Screenshot 2020-11-18 at 9 20 44 AM

@skomis-mm
Copy link
Contributor

@murugaratham just remove this line
var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);

@murugaratham
Copy link

Seems working fine and I feel dumb

@eLPe21
Copy link

eLPe21 commented Nov 19, 2020

Not sure if this is related, but when I build to single file for Centos7 ( the build is done on Mac )
I get extra files that I have to send over to make the application working:

The files are added to the publish folder as soon as I add Serilog.Settings.Configuration on my net50 project.

../out50/System.Native.a			
../out50/System.Native.so			
../out50/System.Security.Cryptography.Native.so

@nathanpovo
Copy link

nathanpovo commented Dec 29, 2020

Another workaround is to set IncludeAllContentForSelfExtract to true when publishing. Setting this property to true changes the .NET 5.0 system of single-file apps to be backward compatible with the .NET Core 3.x system (as described here).

I have tested this with version 3.2.0-dev-00264 of Serilog.Settings.Configuration and it works; version 3.1.0 will not work.

Some additional info on the workaround can be found here - dotnet/runtime#44511 (comment)
Related issue - dotnet/runtime#42265

@sungam3r
Copy link
Contributor

Would be nice to mention it in the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants