peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.33k stars 202 forks source link

.net 5:CodeBase is not supported on assemblies loaded from a single-file bundle #1003

Closed avriltank closed 2 years ago

avriltank commented 2 years ago
Application startup 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.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Peachpie.AspNetCore.Web.PhpHandlerMiddleware.LoadScriptAssemblies(PhpOptions options)
   at Peachpie.AspNetCore.Web.PhpHandlerMiddleware..ctor(RequestDelegate next, IHostingEnvironment hostingEnv, IServiceProvider services, PhpHandlerConfiguration configuration)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass5_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()

The problem occurred in Net5 or above, which is a comparison of Microsoft's official website Net core URL link

jakubmisek commented 2 years ago

thank you, may I ask for more details? What was the Assembly assembly if you check the Locals when this happens.

avriltank commented 2 years ago

Sorry, I don't quite understand what you mean! This is my command to issue a single file:

dotnet publish -c release -r win-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true --self-contained true -o publish_ dir_ win_ single.

For the source code of peachpie, I haven't had time to read it! I think it seems that the incompatibility problem is caused by the code shown in the figure below: one two

jakubmisek commented 2 years ago

The exception is thrown by DeendencyContext.Default.

I have found a workaround on StackOverflow at https://stackoverflow.com/questions/64818343/publishing-error-for-single-file-net-5-project-due-to-3rd-party-dependencies

Although from the exception it seems, /p:PublishSingleFile=true won't allow us to do what we want to do :) I'll try to workaround it. For now, omitting PublishSingleFile might help.

avriltank commented 2 years ago

Thanks for you!This is a great project that will have high performance Net and simple and fast PHP. I'm trying to learn it and introducing it to my friends At least now it performs very well in synchronous code, and I look forward to the asynchronous feature

jakubmisek commented 2 years ago

thank you!

vvtll commented 2 years ago

There is no result when i build a single exe for .net core web project!

avriltank commented 2 years ago

There is no result when i build a single exe for .net core web project!

Hi,You should add script reference for your php library when publish a single file,like this

Context.AddScriptReference(System.Reflection.Assembly.Load("phpapp"));