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

cs-script does not support MAUI Android #379

Closed
kimdiego2098 opened this issue Jul 18, 2024 · 6 comments
Closed

cs-script does not support MAUI Android #379

kimdiego2098 opened this issue Jul 18, 2024 · 6 comments

Comments

@kimdiego2098
Copy link

I tried using this code and found an error in maui Android

CSScript.Evaluator.Eval("6/3");

Error message:

System.Reflection.TargetlnvocationException:Exception has been thrownby the target of an invocation.
: 
SystemNotlmplementedException: The method or operation is not implemented.
at 
System.Reflection.Assemblyget_GlobalAssemblyCache()
at
CSScriptLib.EvaluatorBase~1.<>c

So cs-script support maui or not?

@oleg-shilo
Copy link
Owner

A simple answer is no, your environment does not support scripting. It's not even CS-Script it is your .NET runtime that does not support Roslyn and other more advanced features present in other .NET distributions.

Though it is my educated guess, not the knowledge as I cannot test it right now.

I suggest a very simple experiment. You can disable this very valid call by disabling referencing appdomain assembly:

CSScript.EvaluatorConfig.ReferenceDomainAssemblies = false;

This will let your through the failings step but most likely it will fail when it tries to invoke Roslyn.

Try and let me know how did you go.

And it's not for CS-Script reason but for .NET.

@kimdiego2098
Copy link
Author

CSScript.EvaluatorConfig.ReferenceDomainAssemblies = false;

After configuration, it can take effect and run normally in Android.

How was this achieved?

@kimdiego2098
Copy link
Author

What impact will this config ? I should understand it to prevent errors in other features of the project

@oleg-shilo
Copy link
Owner

How

After configuration, it can take effect and run normally in Android.

Wow, excellent, It's not what I expected as for years Roslyn was only available on the full scale (e.g. desktop/server) runtimes.

The setting that you changed controls automatic assembly referencing. IE by default your script automatically references all assemblies currently loaded in the app domain including the host (your application). This way your script can call any method from your host app without any extra effort.

When the auto referencing is is performed all AppDomain assemblies analyzed and global cache assemblies are filtered out. You do not need to reference them as they are always available anyway. The filtering involves checking the property Assembly.GlobalAssemblyCache and... on Android this property is not implemented and throws the exception. Thus by switching off the whole auto referencing AppDomain assemblies you simply prevented the execution route that calls Assembly.GlobalAssemblyCache.

The impact
Because AppDomain assemblies are no longer referenced you have to reference them explicitly if you need to call any method of that assembly type.

You can either reference all of them explicitly but when you do that you need to ensure that the GAC assessment is not performed. The appropriate call for that is this:

dynamic script = CSScript.RoslynEvaluator
                         .ReferenceDomainAssemblies(DomainAssemblies.AllStatic)
                         .LoadMethod(@"public (int, int) func()
                                        {
                                            return (0,5);
                                        }");

(int, int) result = script.func();

Or you can reference one by one only the assemblies that you need. This is how you can reference host assembly (executing assembly):

dynamic script = CSScript.RoslynEvaluator
                         .ReferenceAssembly(Assembly.GetExecutingAssembly())
                         .LoadMethod(@"public (int, int) func()
                                        {
                                            return (0,5);
                                        }");

(int, int) result = script.func();

Nevertheless, it is a defect. The script engine should handle the cases when Assembly.GlobalAssemblyCache is not implemented.
I have done the fix already so I will be publishing the HotFix patch tomorrow.

@kimdiego2098
Copy link
Author

kimdiego2098 commented Jul 19, 2024

thanks, it solved my doubts!

oleg-shilo added a commit that referenced this issue Jul 19, 2024
- #379: cs-script does not support MAUI Android
@oleg-shilo
Copy link
Owner

Done.
You can update your package:

dotnet add package CS-Script --version 4.8.18-HotFix

oleg-shilo added a commit that referenced this issue Aug 10, 2024
---
## Changes

### CLI
- #378: Bump Request -> Microsoft.CodeAnalysis.CSharp.Scripting 4.10.0
- Added custom command `-set-rt-self` for setting the cs-script engine target runtime to the currently active .NET configuration.

### CSScriptLib
- #378: Bump Request -> Microsoft.CodeAnalysis.CSharp.Scripting 4.10.0
- #379: cs-script does not support MAUI Android
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants