toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 261 forks source link

there is a bug when our project contains an unmanaged dll. #371

Closed introspection3 closed 3 years ago

introspection3 commented 3 years ago

Describe the bug there is a bug when our project contains an unmanaged dll. System.NullReferenceException:“Object reference not set to an instance of an object.” To Reproduce Steps to reproduce the behavior: project contains a unmanaged dll, such as https://github.com/bp74/Zstandard.Net Expected behavior A clear and concise description of what you expected to happen.

Information (please complete the following information):

Additional context Add any other context about the problem here.

jzabroski commented 3 years ago

Can you provide more of the stack trace?

jzabroski commented 3 years ago

@HamiltonManalo Does your fix for #352 address this?

HamiltonManalo commented 3 years ago

@jzabroski It may allow a work around by excluding those particular assemblies. @564064202 Could you give an example package/dll you're running into issues with and I will see if I can test against that?

Thanks.

introspection3 commented 3 years ago

@HamiltonManalo Hi,so sorry ,I'm late. you can use .net core with MySql.Data(Contains libzstandard)

HamiltonManalo commented 3 years ago

@564064202 I tried to recreate it by adding RazorLight and the Nuget package and compiling templates but wasn't able to recreate it. Could you provide more details on where/how the error occurs and a stack trace? Does it look anything like the stack trace here #352?

introspection3 commented 3 years ago

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=RazorLight StackTrace: at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate) at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmit(RazorLightProjectItem projectItem) at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at RazorLight.EngineHandler.<CompileTemplateAsync>d__15.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at RazorLight.EngineHandler.d191.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Common.WebApi.ApiDefaultController.d3.MoveNext() in Api\ApiDefaultController.cs:line 120

introspection3 commented 3 years ago

image It may allow a work around by excluding those particular assemblies-->NOT WORK

introspection3 commented 3 years ago

@HamiltonManalo HI?HOW TO RESOLVE IT

introspection3 commented 3 years ago

@jzabroski sir ,how to resove it

jzabroski commented 3 years ago

@564064202 I'm not sure yet. I am releasing 2.0.0-beta10 now, so hopefully that helps you out. You would have to do the guidance in #352 as part of trying it out.

jzabroski commented 3 years ago

@564064202 I just pushed beta10 up now - it should show up in your feed within the next hour. The underlying theory as to why #352 would help you is it would allow you to exclude arbitrary assemblies from the compilation context. It should work if the issue is identical to #352 where the windows metadata dll is also unmanaged.

HamiltonManalo commented 3 years ago

@564064202 Unfortunately I wasn't able to recreate the bug by adding MySQL.Data. Could you reproduce the bug in a simple project and provide a repo for it? If you can I will look into it further.

codeneno commented 3 years ago

I see, you can use ZStandardNet

codeneno commented 3 years ago

when project contains an unmanaged dll

jzabroski commented 3 years ago

Are you referring to this package https://github.com/bp74/Zstandard.Net

codeneno commented 3 years ago

yes ,we pack mysql.data( own),and mysql.data refer it (contains libzstd.dll)

introspection3 commented 3 years ago

@jzabroski Have you fixed it?

jzabroski commented 3 years ago

@564064202 I will try to look at this issue this week.

@HamiltonManalo Do you have your code around still from your failed repro attempt? That would help jump start my investigation, thank you.

jzabroski commented 3 years ago

@codeneno Silly question, but just so we're comparing apples to apples, what version of https://github.com/bp74/Zstandard.Net are you reproducing your problem with? Which TargetFramework? @564064202 Same - what version of Mysql.Data? I think they're removing some unmanaged dependencies, although I don't know how much remains unmanaged. Your csproj has <TargetFramework>.netcoreapp3.1</TargetFramework> and PackageReferences to Mysql.Data and RazorLight directly?

codeneno commented 3 years ago

My csproj has .netcoreapp3.1 and PackageReferences to Mysql.Data and RazorLight directly.

MySQL.data verison 8.0.21

jzabroski commented 3 years ago

@codeneno Please look at the master branch (above commit hash tag for specific moment in time). I cannot reproduce this issue. Can you please play with the sandbox and submit a PR with an example where this is broken?

I have a feeling that you might be using ServiceCollection incorrectly, and that your ServiceCollection is pulling in eagerly all assemblies. You can use MetadataLoadContext which is the reflective dual to AssemblyLoadContext for choosing what assemblies to load.

jzabroski commented 3 years ago

I added another sandbox example where I explicitly instantiate ZStandardDictionary and load the sample file Bernhard provides in https://github.com/bp74/Zstandard.Net/blob/master/Zstandard.Net.Sandbox/loremipsum.zdict - still no repro.

jzabroski commented 3 years ago

Added a third example where no repro is possible.

softlgl commented 3 years ago

@jzabroski in DefaultMetadataReferenceManager.cs

public IReadOnlyList<MetadataReference> Resolve(Assembly assembly)
        {
            var dependencyContext = DependencyContext.Load(assembly);

            return Resolve(assembly, dependencyContext);
        }

DependencyContext.Load method All the dependent packages will be loaded in,If you include unmanaged assemblies, they will also be loaded。

jzabroski commented 3 years ago

@softlgl Can you please edit the csproj - I posted three examples where I couldn't get it to fail. Including a ridiculously unnecessarily complicated one where I tried everything I could to break it. I even used two different versions of Zstd.

I think, most likely, this bug is somewhere in your code, and not RazorLight. But if you have a repro, I will re-open. Thank you.

jzabroski commented 3 years ago

I forgot to check in loremipsum.zdict for examples 2 and 3. It is in master now. Sorry about that.

jzabroski commented 3 years ago

Unpinning and locking thread. Please open a new ticket with a minimal repro if you still believe RazorLight is at fault.