oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.56k stars 234 forks source link

`CSScript.Evaluator.LoadCode` broken on net48 after cs-script 4.8.3 #362

Closed Cryolitia closed 5 months ago

Cryolitia commented 5 months ago

I have been maintaining an old WPF program. I tried to upgrade NuGet packages today, and CSScript.Evaluator.LoadCode occurred the exception below. After a quick bisect, I found that it works with cs-script 4.8.1 and broken with cs-script-4.8,3. I have tried CSScript.Evaluator.Eval with 4.8.3 but the same fault.

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=未能加载文件或程序集“System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。
  Source=CSScriptLib
  StackTrace:
   在 CSScriptLib.RoslynEvaluator.Compile(String scriptText, String scriptFile, CompileInfo info)
   在 CSScriptLib.EvaluatorBase`1.CompileCode(String scriptText, String scriptFile, CompileInfo info)
   在 CSScriptLib.EvaluatorBase`1.LoadCode(String scriptText, Object[] args)
oleg-shilo commented 5 months ago

Sadly Roslyn can no longer work on .NET Framework. With the latest releases of CS-Script I had to upgrade Roslyn packages and it requires the new version of System.Runlime.Loader that is not available for .NET Framework hosting environment.

.NET team admitted that they should have raised NotSupportedException instead of FileNotFoundException. You can find more info here: https://github.com/dotnet/runtime/issues/22732

What it means is that it's not possible to actually fix it and your options are one of these:

I know it's not the answer you were looking for but it is the unfortunate consequence of relying on some legacy stuff (e.g. .NET Framework). I have also been the victim of that.

I have updated the old code samples with the description of the problem.

Cryolitia commented 5 months ago

So sad about it, I would consider to upgrade .Net, but still wish to support Windows 7 users. /sad

oleg-shilo commented 5 months ago

I though you could have .NET (core) on Win7. Am I wrong?

Cryolitia commented 5 months ago

I though you could have .NET (core) on Win7. Am I wrong?

With a runtime even bigger than my program itself. Thank for your help, I would consider it.

oleg-shilo commented 5 months ago

I forgot another option:

If you do not care about new C# syntax (C#6 and higher) then you can achieve a performance that is even better than on .NET Core (no extra dependency required):

image

I just committed the change required for that. Will release the feature next week.

Cryolitia commented 5 months ago

that's great

oleg-shilo commented 5 months ago

I have reopened this issue and will close it after the update is released. I have done the actual change so it is only a matter of making the release. The solution will is captured in this sample: https://github.com/oleg-shilo/cs-script/blob/master/src/CSScriptLib/src/Client.NET-Framework%20(CodeDom)/Program.cs

oleg-shilo commented 5 months ago

Done: https://www.nuget.org/packages/CS-Script/4.8.14

dotnet add package CS-Script --version 4.8.13

Cryolitia commented 5 months ago

With 4.8.14.0 occures this fault

Unable to load file or assembly or one of its dependencies. Unsupported operation.

image

test code

_binding.FileProcessor = (IFileProcessor) CSScript.CodeDomEvaluator
                 .WithRefAssembliesFilter(asms => asms.Where(a => !a.EndsWith("System.Core.dll") &&
                                                                  !a.EndsWith("Systemdll"))).LoadCode(code);
oleg-shilo commented 5 months ago

This is now your functional error, not the assembly loading one.

Use the example I shared as a starting point and you will see what in your code is causing the problem. Because of the "unsupported operation" you may be trying to execute the script that requires the assembly you have not referenced.

Cryolitia commented 5 months ago

image

it doesn't work, I put the demo code in System.Windows.Application::OnStartup

oleg-shilo commented 5 months ago

Sorry, I did not want you to put the sample code to your project as your project may exhibit its own problems. I suggested that you try the actual code sample - the whole project from the repository.

Anyway, if you look at your own screenshot you will see that your project exhibits problems with duplicated assemblies (ambiguous referencing). Even though I cannot read your message box (I assume it is Chinese), I guess it complains about ambiguous assembly referencing. And it lists the assemblies that you have to exclude to avoid ambiguity.

Thus you need to update your .WithRefAssembliesFilter() call accordingly.

Just to help you with that I attached a standalone project that works. You can start from there.

It also shows that the deployment of CS-Script can be very minimalistic; image

WpfApp2.zip

Cryolitia commented 5 months ago

resolved, thx.