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

Publish fails when blazor app uses BlazorWasmAntivirusProtection Nuget in the project #22

Closed
fingers10 opened this issue Nov 2, 2022 · 22 comments

Comments

@fingers10
Copy link
Contributor

fingers10 commented Nov 2, 2022

Sometimes blazor apps dlls are flagged as false positive and blocked by firewalls or antiviruses. There is already an issue 36978 tracking this.

Meanwhile there is an another great library BlazorWasmAntivirusProtection which is used to overcome this false flagging of blazor apps by antivirus or firewall.

When we use this BlazorWasmAntivirusProtection library and try to prerender, publish fails!!!.

Here is the repo - BlazorWasmAVPrerender to replicate the issue.

Error log:

BlazorWasmAVPrerender -> /home/runner/work/BlazorWasmAVPrerender/BlazorWasmAVPrerender/dist/BlazorWasmAVPrerender/
  Unhandled exception. System.Exception: Could not load the assembly "BlazorWasmAVPrerender" in search directories below.
  /home/runner/work/BlazorWasmAVPrerender/BlazorWasmAVPrerender/dist/BlazorWasmAVPrerender/wwwroot/_framework
  (pwd: /home/runner/work/BlazorWasmAVPrerender/BlazorWasmAVPrerender/BlazorWasmAVPrerender)
   ---> System.BadImageFormatException: Could not load file or assembly 'BlazorWasmAVPrerender, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
  
  File name: 'BlazorWasmAVPrerender, Culture=neutral, PublicKeyToken=null' ---> System.BadImageFormatException: Bad IL format.
     at System.Runtime.Loader.AssemblyLoadContext.InternalLoad(ReadOnlySpan`1 arrAssembly, ReadOnlySpan`1 arrSymbols)
     at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly, Stream assemblySymbols)
     at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly)
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.CustomAssemblyLoader.LoadAssemblyFrom(String assemblyDir, String assemblyName) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\CustomAssemblyLoader.cs:line 36
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.CustomAssemblyLoader.<>c__DisplayClass1_0.<.ctor>b__1(String dir) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\CustomAssemblyLoader.cs:line 20
     at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
     at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
     at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
     at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.CustomAssemblyLoader.<.ctor>b__1_0(AssemblyLoadContext context, AssemblyName name) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\CustomAssemblyLoader.cs:line 19
     at System.Runtime.Loader.AssemblyLoadContext.GetFirstResolvedAssemblyFromResolvingEvent(AssemblyName assemblyName)
     at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName)
     at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
     at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
     at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
     at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.CustomAssemblyLoader.LoadAssembly(String assemblyName) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\CustomAssemblyLoader.cs:line 49
     --- End of inner exception stack trace ---
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.CustomAssemblyLoader.LoadAssembly(String assemblyName) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\CustomAssemblyLoader.cs:line [55](https://github.com/fingers10/BlazorWasmAVPrerender/actions/runs/3374441807/jobs/5600065526#step:7:56)
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.Program.BuildPrerenderingOptions(CustomAssemblyLoader assemblyLoader, CommandLineOptions commandLineOptions) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\Program.cs:line 103
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.Program.Main(String[] args) in C:\Projects\My\Blazor\BlazorWasmPreRendering.Build\BlazorWasmPreRendering.Build\Program.cs:line 28
     at Toolbelt.Blazor.WebAssembly.PrerenderServer.Program.<Main>(String[] args)
  Aborted (core dumped)

BlazorWasmAvPrerender GitHub Actions Link to check additional details if needed

@jsakamoto
Copy link
Owner

@fingers10 Thank you for reporting!
I'm investigation this issue. It must be tough work, but I'll try to resolve the problem.

@jsakamoto
Copy link
Owner

@fingers10
Could you try out the ver.2.0.0-preview.4?

@fingers10
Copy link
Contributor Author

fingers10 commented Nov 3, 2022

@jsakamoto , I tried the new package. Publish is successful. Here is the link.

However, I noticed two issues.

  1. Error related to brotliloader.min.js

image

  1. Navigation makes full page reload. Try clicking on any links in the link.

  2. Counter button click not working.

image

@jsakamoto
Copy link
Owner

@fingers10
Actually, I found that that problem is careless of the BlazorWasmAVPrerender side. Fortunately, I also found a workaround. Could you try out the workaround I sent you as a pull request?

@fingers10
Copy link
Contributor Author

@fingers10
Actually, I found that that problem is careless of the BlazorWasmAVPrerender side. Fortunately, I also found a workaround. Could you try out the workaround I sent you as a pull request?

@jsakamoto lemme check and get back. Meanwhile with due diligence can we raise this issue in respective repo if it can help other consumers?

@jsakamoto
Copy link
Owner

@fingers10
Hi,

Meanwhile with due diligence can we raise this issue in respective repo if it can help other consumers?

Yes, I strongly agree with that your opinion.
So, I created and sent a pull request to the author of the "BlazorWasmAntivirusProtection" to resolve this problem.

I hope the pull request above will be accepted and resolve the problem.

@fingers10
Copy link
Contributor Author

@jsakamoto BlazorWasmAntivirusProtection Package approved the PR and published it. Now the workaround that you have suggested in the .csproj in the sample repo is not needed right?

  <Target Name="_RecompressBlazorBootJson" AfterTargets="_ChangeDLLFileExtensions">

    <ItemGroup>
      <_BlazorBootJsonToRecompress Include="$(PublishDir)wwwroot\_framework\blazor.boot.json"></_BlazorBootJsonToRecompress>
    </ItemGroup>

    <GZipCompress
        Condition="!Exists('$(PublishDir)wwwroot\_framework\blazor.boot.json.gz')"
        FilesToCompress="@(_BlazorBootJsonToRecompress)"
        OutputDirectory="$(PublishDir)wwwroot\_framework\">
      <Output TaskParameter="CompressedFiles" ItemName="_BlazorBootJsonGZipRecompressed" />
    </GZipCompress>
    <Move SourceFiles="@(_BlazorBootJsonGZipRecompressed)" DestinationFiles="$(PublishDir)wwwroot\_framework\blazor.boot.json.gz" />

    <BrotliCompress
        Condition="!Exists('$(PublishDir)wwwroot\_framework\blazor.boot.json.br')"
        FilesToCompress="@(_BlazorBootJsonToRecompress)"
        OutputDirectory="$(PublishDir)wwwroot\_framework\"
        CompressionLevel="$(_BlazorBrotliCompressionLevel)"
        SkipIfOutputIsNewer="$(_BlazorWebAssemblyBrotliIncremental)"
        ToolAssembly="$(_BlazorWebAssemblySdkToolAssembly)"
        ToolExe="$(_DotNetHostFileName)"
        ToolPath="$(_DotNetHostDirectory)">
      <Output TaskParameter="CompressedFiles" ItemName="_BlazorBootJsonBrotliRecompressed" />
    </BrotliCompress>
    <Move SourceFiles="@(_BlazorBootJsonBrotliRecompressed)" DestinationFiles="$(PublishDir)wwwroot\_framework\blazor.boot.json.br" />

  </Target>

@jsakamoto
Copy link
Owner

@fingers10 Sorry too late since I was busy watching the .NET Conf 2022 that was held this week. (I live in Japan, so I had to wake up at midnight in the Japan Standard Time zone and keep staying up).

Now the workaround that you have suggested in the .csproj in the sample repo is not needed right?

Yes, that's right, it should work well even after removing that workaround.
However, please remember that I've not verified it yet in my hand.

@fingers10
Copy link
Contributor Author

fingers10 commented Nov 12, 2022

@fingers10 Sorry too late since I was busy watching the .NET Conf 2022 that was held this week. (I live in Japan, so I had to wake up at midnight in the Japan Standard Time zone and keep staying up).

Now the workaround that you have suggested in the .csproj in the sample repo is not needed right?

Yes, that's right, it should work well even after removing that workaround.
However, please remember that I've not verified it yet in my hand.

@jsakamoto No worries. Yep I know that you are watching .NET Conf 22. I saw your question on C# required properties. Your question was too specific on adding required to property injection in blazor. Good question.

Back to this issue. I'll validate this and get back.

@fingers10
Copy link
Contributor Author

@jsakamoto Just confirmed. Prerendering is working with BlazorWasmAntivirusProtection 1.9.0 without the work around in .csproj file.

@fingers10
Copy link
Contributor Author

fingers10 commented Nov 20, 2022

@jsakamoto I noticed that publish fails when we have lazy loading enabled with BalzorWasmAntivirusProtection. The issue happens in the same sample repo. Please can we reopen and investigate further?

@jsakamoto
Copy link
Owner

@fingers10 I'm currently investigating the revival problem. I'll inform you the progress as soon as I can.

@jsakamoto
Copy link
Owner

@fingers10 The latest version of the "BlazorWasmPreRendering.Build" has now supported the latest version of the "BlazorWasmAntivirusProtection". Please check it out.

However, your sample project still failed because the project uses the "lazy assembly loading" feature of Blazor WebAssembly.

image

Again, the "BlazorWasmPreRendering.Build" depends on the pre-rendering feature of the Blazor Server. The "BlazorWasmPreRendering.Build" is implemented pre-rendering feature by making a Blazor WebAssembly app run as a Blazor Server app forcibly with some hacking. But these magics are not applicable for the "lazy assembly loading" feature because that feature is very specific on the client side running.

I'll try to add support for the "lazy assembly loading" feature to the "BlazorWasmPreRendering.Build" later, but it must be very tough work, and please keep your mind that it might be impossible.

@jsakamoto
Copy link
Owner

@fingers10 Now the latest version of the "BlazorWasmPreRendering.​Build" has supported lazy loading assemblies! 🎉

Could you check it out? and, thank you for giving me the big challenge that is adding support for lazy loading assemblies!

@fingers10
Copy link
Contributor Author

@jsakamoto salute to you. You are making a great solutions. You earned my respect a lot. I'll check this and keep you posted

@fingers10
Copy link
Contributor Author

@jsakamoto I have noticed few observations.

  1. Publish keeps running when I publish via Visual Studio 2022 v 17.4.1 . I ended up terminating manually. Is this switch turned on by default or any new settings needs to be configured to cancel this?

The pre-rendering server will keep running because the "-k" option switch is specified.

logs:

BlazorWasmAVPrerender -> C:\Repos\BlazorWasmAVPrerender\BlazorWasmAVPrerender\obj\Release\net7.0\browser-wasm\PubTmp\Out\
dotnet "C:\Users\abcd\.nuget\packages\blazorwasmprerendering.build\2.0.0-preview.7\build\../tools/net7.0/BlazorWasmPreRendering.Build.dll" -a "BlazorWasmAVPrerender" -t "BlazorWasmAVPrerender.App" --selectorofrootcomponent "#app,app" --selectorofheadoutletcomponent "head::after" -p "C:\Repos\BlazorWasmAVPrerender\BlazorWasmAVPrerender\obj\Release\net7.0\browser-wasm\PubTmp\Out" -i "C:\Repos\BlazorWasmAVPrerender\BlazorWasmAVPrerender\obj\Release\net7.0\." -m "" -f "net7.0" --serviceworkerassetsmanifest "" --environment "Prerendering" --emulateauthme "true" --locale "en" -o "IndexHtmlInSubFolders"  -k -d -u "" -r "WebAssemblyPrerendered" --serverport "5050-5999" --bwapoptionsdllext "dat"
Start fetching...[http://127.0.0.1:5050]
Getting http://127.0.0.1:5050/...
Getting http://127.0.0.1:5050/counter...
Getting http://127.0.0.1:5050/fetchdata...
Fetching complete.

The pre-rendering server will keep running because the "-k" option switch is specified.
To stop the pre - rendering server and stop build, press Ctrl + C.
Build has been canceled.
========== Elapsed 15:01.876 ==========
  1. Tried publishing via github actions - link. This also keeps on running but not displayin any logs.

@jsakamoto
Copy link
Owner

@fingers10
It seems because you specified the BlazorWasmPrerenderingKeepServer MSBuild property to true explicitly, doesn't it?

https://github.com/fingers10/BlazorWasmAVPrerender/blob/master/BlazorWasmAVPrerender/BlazorWasmAVPrerender.csproj#L14

@fingers10
Copy link
Contributor Author

@jsakamoto ah ha.. I'm wondering how this flag was set to true in the codebase. I couldn't remember...but history shows this was added along with Nuget Update commit. Never mind.

It's good it works with simple scenarios.. lemme check with production project and share my updates.

@fingers10
Copy link
Contributor Author

@jsakamoto for some reason the publish fails in github actions deploy. Here is the link.

Error:

The type initializer for 'Brotli.Brolib64' threw an exception

Logs:

/home/runner/.nuget/packages/blazorwasmantivirusprotection/2.2.1/build/net7.0/BlazorWasmAntivirusProtection.targets(36,3): error : The type initializer for 'Brotli.Brolib64' threw an exception. [/home/runner/work/ilovedotnet/ilovedotnet/Web/Web.csproj]
  BlazorWasmAntivirusProtection: Renaming .dll files to .dat finished
Error: Process completed with exit code 1.

@jsakamoto
Copy link
Owner

@fingers10 I could not reproduce the error with that I forked the "ILoveDotNet/ilovedotnet" repository, hard reset the main branch point to the commit 66c8265f, and re-run the gh-pages workflow forcibly. (The log line is here)

Moreover, the error you reported seems telling us that the reason of the error comes from the "BlazorWasmAntivirusProtection" package, not the "BlazorWasmPreRendering.Build" package.

I would like to recommend:

  • Try again with re-enabling the "BlazorWasmAntivirus". If there was no error, I guess that the error may be temporary, and it may depend on some environmental factors of GitHub Actions virtual machine.
  • If the same error came back again, then please remove the "BlazorWasmPreRendering.Build" package from project and try again. If the same error was still there, the error must be from the "BlazorWasmAntivirus", so prelase report it to the issue thread of the "BlazorWasmAntivirus" repository.

@fingers10
Copy link
Contributor Author

@jsakamoto Many thanks for your suggestions and guidance. This thread can be closed.

@jsakamoto
Copy link
Owner

Thanks!

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

No branches or pull requests

2 participants