oleg-shilo / cs-script

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

My script runs fine in VS2013 but not with CSScript - Can't find custom attr constructor image #115

Closed AlGantori closed 6 years ago

AlGantori commented 6 years ago

Background: I recently switched to VSCode (cause I've been doing some mobile hybrid development with ionic framework) CSScript in Notepad++ quit working a while back, so I have been running my C# scripts via VS2013 however, I like keeping them running in both environment. While visiting CSScript sites I was happy to see you have moved to GitHub and also support VSCode.

Here is the console output in VSCode, I can't understand what is it complaining about PresentationFramework?

[Running] mono "C:\Users\AXM\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "s:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\DeployToProduction.cs"
Can't find custom attr constructor image: S:\_W\CONTROLS.WPF\VRQS\SRC\QSData.WPF\bin\Debug\VR.QS.Data.WPF.dll mtoken: 0x0a00000b due to: Could not load file or assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. assembly:PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:<unknown type> member:(null) signature:<none>
Quran.Load resources failed
Quran.Load resources failed
QuranMETA.Load resources failed
Error: Specified file could not be executed.

System.NullReferenceException: Object reference not set to an instance of an object
  at VR.PPTLibs.PPT_Export.FieldValues (System.String FieldName) [0x0000e] in <da5a43c3f0bd477480231f6a007edea9>:0 
  at ScriptClass.MakeXML () [0x00053] in <222c7a09ce904a66b56da6882f54b05c>:0 
  at ScriptClass.main () [0x0011b] in <222c7a09ce904a66b56da6882f54b05c>:0 
  at ScriptClass.Main (System.String[] args) [0x00005] in <222c7a09ce904a66b56da6882f54b05c>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <9c781d3eb98b4df498100896b80cbd27>:0 

[Done] exited with code=1 in 2.577 seconds

I have the following VSCode extensions installed image

From VS2013 the same script is file linked and works fine. image

I have a simple button on a WPF window to quick off main like so image

The Output window in VS2013 shows none of the errors in the VSCode console above, upon running the TESTER WPF Window, I get the following output and if I actually execute main() of the script it runs as expected.

The thread 0x454 has exited with code 259 (0x103).
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'S:\_W\PULIB4\SRC.NET\VRUSLIB.TESTER\VRUSLIB.TESTER\bin\Debug\VRUSLIB.TESTER.exe'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXmlLinq\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXmlLinq.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXml\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXml.dll'. Symbols loaded.
'VRUSLIB.TESTER.vshost.exe' (CLR v4.0.30319: VRUSLIB.TESTER.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationProvider.dll'. Symbols loaded.
The thread 0x3dd0 has exited with code 0 (0x0).

The actual script image

image

I have one assembly in VB.NET, and the remaining in C# / WPF Let me know what else you need. Thank you.

AlGantori commented 6 years ago

If I make main() empty like so image

I get only the first error

[Running] mono "C:\Users\AXM\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "s:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\DeployToProduction.cs"
Can't find custom attr constructor image: S:\_W\CONTROLS.WPF\VRQS\SRC\QSData.WPF\bin\Debug\VR.QS.Data.WPF.dll mtoken: 0x0a00000b due to: Could not load file or assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. assembly:PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:<unknown type> member:(null) signature:<none>
Quran.Load resources failed

[Done] exited with code=0 in 0.954 seconds

Here are the references in project/assembly VR.QS.Data.WPF
image

oleg-shilo commented 6 years ago

Hi there,

As for Notepad++ there were nasty problems with N++ plugins repository as well as plugin manager deployment bugs. I have lost the track of how many unfortunate hiccups associated with N++. Though about half year ago CS-Script plugin for N++ has been re-released with support for both x86 and x64 N++ editions and since then it all seams OK.

Anyway, sorry but I cannot make sense from the multiple screenshots you provided. It's not clear what is your script and how you execute it. Do you host the script engine or you execute the whole WPF app as a script?

There is one thing is an immediate candidate to check though. You probably noticed that you are executing the script/application with Mono. This is the default VSCode runtime for the desktop apps.

Though CS-Script lets you fallback to .NET via the settings. Try this:

"cs-script.dotnet_run_host_on_win": true,

If this does not help then please compose a simple HelloWorld style project that exhibits the problem and I will have a look at it.

AlGantori commented 6 years ago

You say "mono" is it because of the installed VSCode "mono debug" extension? or because it shows in the console as "...System.Reflection.MonoMethod....." Sorry for my ignorance :)

Where does your suggested setting above go? Is it in settings.json, *.xml, in the code? I looked in the Wiki but could not find anything about "cs-script.dotnet_run_host_on_win" BTW, I was not aware of the existence of C:\Users\AXM\AppData\Roaming\Code\User\cs-script.user\

Regarding how I run the code.. I have over many years built different functional blocks each providing things like PowerPoint automation, XML generation, etc... I reference these assemblies from the scripts which I typically call DeployToProduction.cs

I was able to load and execute these scripts in N++ without issues. However, from time to time I need to debug and step thru the actual assemblies. So you (or someone) helped me back in 2014/2015 to define some conditionals and I was able to run in both environments.

Mode1: From VS2013 I have a "tester harness" WPF project referencing all the needed assemblies, I use file linking directly to the desired DeployToProductions.cs of interest and build a simple button into currently a WPF window (instead of Windows Form App). In the button handler, I create an instance of the uniquely named class declared in the script and execute its main as below.

 private void DeployToProduction_QURAN4CHILDREN_AayaByAaya_AMSXML_Click(object sender, RoutedEventArgs e)
        {
            var p = new DeployToProduction_QURAN4CHILDREN_AayaByAaya_AMSXML();
            p.main();
        }

I believe in the above mode CSScript is not involved at all.

Mode2: From N++ and now from VSCode the conditional code allow for referencing via css_ref to the actual assemblies (I am not using GAC as it was too complex, only good for stable assemblies)

This "scripting" mode allows for simple day-to-day operations.

  1. Goto to desired work folder/area
  2. load script file into N++/VSCode and execute it.

I hope this help. I would like to try out your suggested "cs-script.dotnet_run_host_on_win" setting and see if it sheds any light on what Mode2 is chocking about.

I just now noticed there is now a VSCode "CS-SCRIPT - OMNISHARP" folding panel which contains 2 files ".../temp/..." launch.json and script.csproj neither seem to be a candidate for the setting above.

Thanks.

oleg-shilo commented 6 years ago

Yes your info helps.

Let me explain the Mono point first. CS-Script engine executes C# scripts either under .NET or Mono:

cscs.exe script.cs
mono cscs.exe script.cs 

The .NETCore edition is currently under development and not available publicly. It will be invoked like that:

dotnet cscs.dll script.cs 

When you executed your scripts from Notepad++ it was always under .NET full runtime. BTW I think it was me who helped you then : )

VSCode allows C# debugging apps targeting non-Core runtime but only if it is Mono (not .NET full). This is the reasons why CS-Script.VSCode extension runs and debugs scripts under Mono by default. You can override this mode and run scripts under .NET with that setting that I mentioned.

Thus may help as I am not sure Mono is playing nice with WPF and the error output clearly indicates some Mono runtime bindings problems.

cs-script.dotnet_run_host_on_win is an extension setting that is (as with any other extensions) described at the extensions 'Contributions' tab of the extension page:

image

If you want to modify any setting of VSCode you don't do this directy in the file but rather use IDE for that:

Now you will be able to run the scripts exactly the same way as in Notepad++ but you will still have the problem with debugging as it has to be done under Mono, which I suspect is incompatible with your assemblies.

AlGantori commented 6 years ago

If I make main() empty like so image

I get only the first error

[Running] mono "C:\Users\AXM\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "s:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\DeployToProduction.cs"
Can't find custom attr constructor image: S:\_W\CONTROLS.WPF\VRQS\SRC\QSData.WPF\bin\Debug\VR.QS.Data.WPF.dll mtoken: 0x0a00000b due to: Could not load file or assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. assembly:PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:<unknown type> member:(null) signature:<none>
Quran.Load resources failed

[Done] exited with code=0 in 0.954 seconds

Here are the references in project/assembly VR.QS.Data.WPF

AlGantori commented 6 years ago

The setting cs-script.dotnet_run_host_on_win did the trick.

The console now shows to the following:

[Running] "C:\Users\AXM\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "s:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\DeployToProduction.cs"
Copying to...S:\_W\ARP_WEB\SRC\KIDS\QURAN\QURAN4CHILDREN\ALAFASY\093\DARC01.XML
ERROR: The process "POWERPNT.EXE" with PID 2556 could not be terminated.
Reason: There is no running instance of the task.

[Done] exited with code=0 in 4.751 seconds

I got the couple .XML files expected out of this run. The only thing I need to perhaps understand / fix. Is the ERROR: The process "POWERPNT.EXE" with PID 2556 could not be terminated.

As I don't see that error when running from VS2013.

But again automating Office is super challenging, as the office API is totally stupid you tell the app .Quit() and it does not exit. One has to use taskill to blow it off.

Hopefully my scripts run to expected output, and debugging in VS2013 will be sufficient.

Thanks for taking the time to explain. I think I got like 70% understand. Microsoft over the years Windows SDK, C++, MFC, ATL, .NET, Silverlight, WPF (binding only an alien could understand), LINQ, blabla, .... has permanently damaged my neurons and they make me feel "stupidier" :) I am kinda stepping away from .NET and moved to TS/HTML/CSS for app development. However, I have lots of internal tools and assemblies based on .NET (so I am trapped at least for my app content generation) On the node.js camp, these monkeys have created the biggest cancer of the world node_modules, a cult of twisted naming conventions, a CLI, etc :)

BTW, I like the way you have thought ahead and prefixed your settings with cs-script, otherwise it would be a zoo even if there is a search field :) Thanks a bunch.

oleg-shilo commented 6 years ago

Ha-ha.

Your email resonates with me a lot. :)

After using TS in anger (for VSCode extensions) I can honestly say that I really like it but... it comes with the "node_modules" monstrosity and node.js API puzzling nature. Thus I still prefer C# :) And when it comes to Web+C# I am really intrigued by WebAssembly+Xamarin. Hope it truly picks.

Anyway, I am almost sure that active "POWERPNT.EXE" is the result of the script using out-of-process COM object from PowerPoint Automation. I have seen it before. You can probably just Process.GetprocessByName("powerpnt").Kill() it.

I like the way you have thought ahead and prefixed your settings with cs-script,

Yeah, I though it would help. But coming from the background where namespaces are common it was just screaming to do it this way.

Cheers, Oleg

AlGantori commented 6 years ago

That did it I replaced

        ' kill app
        ' 20150321
        ' the above is not working, so I am going with the brute force
        '  http://stackoverflow.com/questions/12163390/close-powerpoint-object-with-excel-vba-not-using-powerpoint-application
        Dim BruteForce As String = "TASKKILL /F /IM powerpnt.exe"
        Shell(BruteForce, vbHide)

with your suggestion like so tweaked into vb, (as this particular assembly is in vb.net)

        ' https://github.com/oleg-shilo/cs-script/issues/115
        ' 20180522 Suggested by Oleg-shilo guru of CSScript
        Dim Ps As Process() = Process.GetProcessesByName("powerpnt")
        For Each p In Ps
            p.Kill()
        Next

No errors in the output

[Running] "C:\Users\AXM\AppData\Roaming\Code\User\cs-script.user\cscs.exe" "s:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\DeployToProduction.cs"
AMSXMLMaker.Make...S:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\ALAFASY\087\BMP\DARC01.XML
AMSXMLMaker.Make...S:\_W\ARP_WEB\RC\KIDS\QURAN\QURAN4CHILDREN\ALAFASY\087\BMP/../PLAY\DARC01.XML
Files.CopyFolderContents...S:\_W\ARP_WEB\SRC\KIDS\QURAN\QURAN4CHILDREN\ALAFASY\087\DARC01.XML

[Done] exited with code=0 in 5.682 seconds

I forgot to mention that after being fed-up with Microsoft promises and Silverlight propaganda, I went to Xamarin. I was there from 2012 but now dropping it as I plan to replace those Google play store productions with Ionic based versions. I have tons of html diaries about that Xamarin/Android development, and it is as ugly as anything.

image image image image

When I decided to leave Xamarin, I think that's when and where Microsoft got in, and it was to adopt XAML for layout, yuck! :). I never could master binding in XAML, nor anyone on stackoverflow. I still use WPF/XAML in some of my internal tools but I would drop them anytime I find a simpler alternative. Stackpanel was a pain. The Grid was OK. Pickup all the Android specific lingo, like activity and it's life cycle, Android's AXML layout language, Inject this/that, etc... was a waste for me. I did not want to pollute my brain with yet an iOS lingo.

On the node.js side I like that you have to master 3 things HTML / CSS and a control language like TS (I would never do JS). JS world is single threaded so they have these things like "Promises" to handle asynchronous calls I still struggle with. There is also Observables, and Async Await to wrap your brain around.

On the layout side, I like the fact that I can tweak dynamically CSS and see results instantaneously in my running app. Even VB could not do that :) I try to use mostly Viewport Units, vmin is my friend. However, I believe CSS needs badly a super overhaul to use simpler and consistent terminology for modern GUI layouts and the average human person.

Thank you for your help. You may close this issue.

oleg-shilo commented 6 years ago

As far as XAML goes I really appreciate it. At least there is a clear recognition that there is a fundamental difference between Application and Document markup. WPF is arguebly the most advanced XAML-based technology but it missed the mark. How naive MS was even trying to convince us that industrial designers would be able to pump XAML and be happy. Styling... great idea to have it in the same language but this in turn failed to promote proper separation between raw layout and style (like CSS does). Ironically Avalonia picked on this and allows plane CSS over XAML.

My next hope is OOUI. Intrigued by it. Though have no confidence to bat on it. But its strong points are impressive. Addressing portability in the most efficient way, XAML, lean code model (no 'node_modules' madness), data binding is still available. If it will get bundled with Calibutrn.Micro... I will be sold completely. But of course it's all irrelevant if it doesn't click with the community.

Nice talking to you :)

Closing this issue....