oleg-shilo / cs-script

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

Migration from a hosted scenario using .net framework 4.0 to .net 6.0 #315

Closed shawn-pierce-intel closed 1 year ago

shawn-pierce-intel commented 1 year ago

Struggling a little with how to move to the latest hosted execution correctly and not rewrite the existing scripts so I was attempting to follow https://github.com/oleg-shilo/cs-script/wiki/Hosted-Script-Execution

This is a typical script I am using in the older version, that I want to load and then I would call the methods ExecuteVerify and ExecuteScript separately BasicScript.txt

Here is how I accomplished this all those years ago private static Assembly LoadCode(ScriptReply reply, string scriptCode, bool logScriptIssues) { Assembly assm = CSScript.LoadCode(scriptCode);

        //Capture and Obsolete Methods and add them to the Scripting Reply
        if (!logScriptIssues) return ass;

        if (CSScript.CompilingHistory.Any())
        {
            foreach (
                var y in
                    CSScriptLibrary.CSScript.CompilingHistory.Last()
                        .Value.Errors.Cast<CompilerError>()
                        .Where(y => y.ErrorText.Contains("obsolete")))
            {
                //Add to the ScriptReply
                reply.LogMessage(string.Format("{0} on script line {1}", y.ErrorText, y.Line),
                    LogLevel.ScriptIssue);
            }
        }
        return assm;
    }

2 problems I see right now ...

dymanic -> assembly not sure this next step to correctly refactor the existing code to utilize this type (less important as ...) When attempting LoadCode I get

image

Thoughts?

oleg-shilo commented 1 year ago

The second problem is simple. As error message (produced by Roslyn) suggests you cannot use namespaces in the script. It is a limitation of Roslyn. So get rid of any namespaces. Alternatively you can use csc as a compiling engine but in this case you will only be able to run your application on the target system where .NET SDK is present. Roslyn allows you to run script with .NET Runtime.

This sample shows the technique. https://github.com/oleg-shilo/cs-script/blob/master/src/CSScriptLib/src/Client.WPF/MainWindow.xaml.cs

It also shows how to compile the script in the assembly.

damian-666 commented 1 year ago

In my case also using WPF. from windows.netcore6 or 7, migrating from Net 4.8 i just had to remove all plugin namespaces, because rosylin was the easiest to get working again with csscrpt. then i got enough working ot move on to the next thorny issue on another aspect. But having stripped the namespaces from plugins it adds a root one. I then faced some Serialization issues wth data types redefined in plugins w/o namespaces. as the plugins in the plugins folder get compiled, but then you cna tweak them or and save data to the plugins owner. But solvable wiht serialization ( datacontract or similar) you can mix and match these adjusttypes named easily , and substitute ( the namespace by defualt will appear in the type) but even At runtime There are advanced typing methods converter methods i havednt had to look into but didnt needethem, that can let you change the name of a type ( i think, and add or remove the css prefix) , but i casted to object , used reflection ,or alignement to interface or something like that to escape it.

BTW since im been struggling 2 montsh i offer some general advice : this is just on the topic of UI, XAML and WPF... (#off topic, and Meta) as usual :) sorrY IF TLDR, i dont know what you are migraating or how big, why , or who else sees this , but moving a big wpf app to net6 can be months of work or bankrupt a company. I don't have a pressing need to target non windows but my other components do. first i treid upgrade assistant., it gave some help but i removed its injected code. I have abandoned docking binary components that still work somehow without its "help" I did wpf = >wpf6core because a key component dropped Net Standard 2 support which is not really supported well anwyas so all core dlls linked to my systgemdirectly had to be net6 not netstanddard2. that ended up in my case worththe pain. I dindt gain perfromance on the wpf itself parts because of all the dispatcher limitations and nuances. but for all htether parts i got immediate huge 40% perfromance gains from net 6/ 7 and potential for generalized SIMD. 2x -10x more. i still use WPF because ti fully open WPF.core source, and i think every other msft UI is a nonstarter or gets killed. this include Maui ( looks nonstarter, bloated lots of trouble and not going well ) UWP is quite dead i ejected early on after wasting months. w WinUI3( I see no point ddint look) . even Silk iwindoing snt going well lately.. ints more bloated than Xamarin. i cant get Xamarin AOT LLVM to worik yet but im staying with it. i do like XAML sort of and heavily invested, I might move to AvaloniaWPF tho. i use some of its pieces. .. mostly theres the loss of Netstandard 2.0 directy compatiliby. in general wpf-netcore6 is naunced but it works. Avalonui i s well over 7 years old and going strong, appears. and thehy can look in the other WPF vault as its MIT license now, i guess not no more reverse engineer ing but they must continue to provide abstactions that MSFT doesnt have. Thats why i look at Silk .net , but I stopped looking because its sort of getting stuck here ant there ,well see..

I faced 6 weeks of issues ,Cscript was relatively minor. so i would kind of take a breath-first approach depending on how much legacy code you have, some companies are going backrupt trying to get off from 4.8. some only have a few weeks work. But with MSFt i think legacy platfroms.. they have a way of not actually dying once they get past 10 or 15 years old. because of industry pressure, while the fledgling UI usually dies young there are dozens of examples if you add the less famous tradegies.

shawn-pierce-intel commented 1 year ago

That is where I ended up today, manually removing the namespace from a couple of small scripts to testing does seem to "initially" work with Rosyln but, like you said I have a long ways to go with identifying the more complicated scripts that we have made over the last 10 years or so.. :)

Thank you both for the responses, made a lot of progress today and am quite happy with at least getting some things working .

Did you manually remove the namespaces or did you use code to do it? Any recommendations on a quick way of removing it via code?