Assembly loading under TextTransform.exe, Mono - c#

I have a .NET assembly, written in C#, which references the Microsoft.CodeAnalysis, Microsoft.CodeAnalysis.CSharp and System.Collections.Immutable assemblies and contains this function:
public string GetFirstNamespace(string code)
{
var tree = CSharpSyntaxTree.ParseText (code);
var root = tree.GetRoot ();
var #using = root.DescendantNodes ().OfType<UsingDirectiveSyntax> ();
var #types = root.DescendantNodes ().OfType<TypeDeclarationSyntax> ();
var #enums = root.DescendantNodes ().OfType<EnumDeclarationSyntax> ();
var #namespaces = root.DescendantNodes ().OfType<NamespaceDeclarationSyntax> ();
return #namespaces.First ().GetText ().ToString();
}
I can call this function from a command-line program written in C#:
public static void Main(string[] args)
{
string code = "using System; namespace Foo { public class Program { public static void Main(string[] args) { Console.WriteLine(\"Hello, World\"); } } }";
Utilities u = new Utilities();
string ns = u.GetFirstNamespace(code);
Console.WriteLine (ns);
}
But if I try to invoke the same method from a T4 template, run using TextTransform.exe, I get into all sorts of bother. Initially, there are a long list of assemblies that it cannot find:
$ /usr/lib/monodevelop/AddIns/MonoDevelop.TextTemplating/TextTransform.exe -P=./bin/Debug T4Template.tt
Processing 'T4Template.tt' failed.
(0,0): ERROR Metadata file `System.Runtime.dll' could not be found
(0,0): ERROR Metadata file `System.Collections.dll' could not be found
(0,0): ERROR Metadata file `System.Runtime.InteropServices.dll' could not be found
(0,0): ERROR Metadata file `System.Resources.ResourceManager.dll' could not be found
(0,0): ERROR Metadata file `System.Globalization.dll' could not be found
(0,0): ERROR Metadata file `System.Linq.dll' could not be found
(0,0): ERROR Metadata file `System.Diagnostics.Debug.dll' could not be found
(0,0): ERROR Metadata file `System.Runtime.Extensions.dll' could not be found
(0,0): ERROR Metadata file `System.Threading.dll' could not be found
(0,0): ERROR Metadata file `System.Diagnostics.Tools.dll' could not be found
(0,0): ERROR Metadata file `System.Reflection.dll' could not be found
Adding one of the installed Mono directories to the assembly search path fixes these, but leads to other problems:
$ /usr/lib/monodevelop/AddIns/MonoDevelop.TextTemplating/TextTransform.exe -P=./bin/Debug -P=/usr/lib/mono/4.5/Facades T4Template.tt
Invalid type Microsoft.CodeAnalysis.SyntaxTree for instance field Microsoft.CodeAnalysis.SyntaxNode:_syntaxTree
Processing 'T4Template.tt' failed.
(0,0): ERROR Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Failure has occurred while loading a type.
at T4CSTest.Utilities.GetFirstNamespace (System.String code) <0x4047b4e0 + 0x0002f> in <filename unknown>:0
at Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation21eb083a.TransformText () <0x4047afa0 + 0x0009b> in <filename unknown>: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, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x7f7d1d530780 + 0x000a1> in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x7f7d1d530780 + 0x000ef> in <filename unknown>:0
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) <0x7f7d1d368cf0 + 0x0002a> in <filename unknown>:0
at Mono.TextTemplating.CompiledTemplate.Process () <0x4047a520 + 0x0036f> in <filename unknown>:0
The same thing works under Visual Studio 2013 on Windows.
I've tried this using the referenced assemblies got from NuGet and also using the Microsoft.CodeAnalysis.* assemblies from https://github.com/mono/roslyn, built using Mono 4.2.1 (with a one-line patch to fix a build error).
My basic question here is, how can I get this to work?
A subsidiary question is, why doesn't it find all those System.* assemblies without help? Shouldn't it find them anyway? Is this because those Microsoft.CodeAnalysis.* assemblies are built against the PCL? Adding in the PCL directory to the search path causes an even longer list of problems, mostly with multiply-defined classes (not too unexpected, really).
Edit Full demonstration code is available at https://github.com/tomkcook/T4CSTest

Related

OleDB in Pythonnet and Mono on Mac OS X

I am trying to run several method of a dll library that I own. I use pythonnet 3.0.0dev, python3.9 and Mono 6.10.0. One of the methods triggers creating an .mdb file. On a windows machine I've install Microsoft Access Database Engine (suggested by the dll provider) and I could run my script without any problem. However, doing the same on a Mac OS X triggers the following error:
NotImplementedException: OleDb is not implemented.
at System.Data.OleDb.OleDbConnection..ctor () [0x0000b] in <bd12f551d3b343c5a146c906291931e8>:0
at (wrapper remoting-invoke-with-check) System.Data.OleDb.OleDbConnection..ctor()
at Csu.Modsim.NetworkUtils.DBUtil.OpenDBConnection () [0x0000b] in <1c83b754d09149ce95a722be79ca7efb>:0
at Csu.Modsim.NetworkUtils.DBUtil.Create (System.Boolean Overwrite, System.Boolean LeaveOpen) [0x00026] in <1c83b754d09149ce95a722be79ca7efb>:0
at Csu.Modsim.NetworkUtils.ModelOutputMSDB.InitializeOutput () [0x000a6] in <1c83b754d09149ce95a722be79ca7efb>:0
at Csu.Modsim.NetworkUtils.ModelOutputSupport.FlushOutputToCSV (Csu.Modsim.ModsimModel.Model mi, System.Boolean finalizeOutput) [0x0030d] in <1c83b754d09149ce95a722be79ca7efb>:0
at Csu.Modsim.ModsimModel.GlobalMembersOperate.operate (Csu.Modsim.ModsimModel.Model mi, Csu.Modsim.ModsimModel.Model mi2) [0x012ab] in <6f164bf9bd4744b9a0c1e4daaabc0e8a>:0
at Csu.Modsim.ModsimModel.Modsim.RunSolver (Csu.Modsim.ModsimModel.Model mi) [0x002f7] in <6f164bf9bd4744b9a0c1e4daaabc0e8a>:0
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <55adae4546cd485ba70e2948332ebe8c>:0
What could be a possible solution to this problem. All my installed components are 64-bit.
Thanks

MissingMethodException in Xamarin.Forms project

I am developing an app using Xamarin.Forms. I have a method which returns all properties of a given object as a string.
public static string GetAllProperties(this object obj)
{
return string.Join(" ", obj
.GetType()
.GetRuntimeProperties()
.Select(prop => prop.GetValue(obj)));
}
It works fine on iOS and UWP, but throws a MissingMethodException on Android when accessing IsTransient property of the given object.
[0:] System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Attempted to access a missing method.
at (wrapper managed-to-native) System.Exception:nIsTransient (int)
at System.Exception.get_IsTransient () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>: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 <896ad1d315ca4ba7b117efb8dacaedcf>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0004b] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Reflection.MonoProperty.GetValue (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00038] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Reflection.PropertyInfo.GetValue (System.Object obj) [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at Fantom.Helpers.Global.GetAllProperties (System.Object obj) [0x00034] in ...
at Fantom.ViewModel.SignUpViewModel+<<FirebaseSignUp>b__20_0>d.MoveNext () [0x00340] in ...
I tried the following with no luck.
Cleaning the solution and restarting Visual Studio.
Changing the .NET framework for the PCL project from 4.5.1 to 4.6 (Project Properties > Library > Targeting > Change...), but even though 4.6 is listed it doesn't let me pick it.
Making sure the latest version of Xamarin NuGet packages are installed on each platform project.
I don't think it would help diagnose the problem, but I have the following NuGet packages installed in the PCL project.
Firebase.Xamarin
Humanizer
Microsoft.Bcl
Microsoft.Bcl.Build
Microsoft.Net.Http
Newtonsoft.Json
PCLAppConfig
PCLStorage
Rx-Core, Rx-Interfaces, Rx-Linq, Rx-Main, Rx-PlatformServices
Xamarin Forms
Platform projects only have the Xamarin packages installed.

Mono.CSharp.Evaluator.ReferenceAssembly() throwing NullReferenceException

Using Mono.CSharp.dll version 2.0 targeting .Net 3.5.
Using the Unity Editor version 5.6.3p1.
There is a using statement for .
Here is my code:
Evaluator.Init(new string[] { });
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
Evaluator.ReferenceAssembly(assembly);
(not shown: using Mono.CSharp;)
Here is the error (throwing on that third line):
NullReferenceException: Object reference not set to an instance of an object
Mono.CSharp.TypeManager.IsThisOrFriendAssembly (System.Reflection.Assembly assembly)
Mono.CSharp.Namespace.RegisterExternalExtensionMethodClass (System.Type type)
Mono.CSharp.RootNamespace.RegisterExtensionMethodClass (System.Type t)
Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType)
Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType)
Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx)
Mono.CSharp.Driver.LoadReferences ()
Mono.CSharp.Evaluator.InitAndGetStartupFiles (System.String[] args)
Mono.CSharp.Evaluator.Init (System.String[] args)
CharlesOlinerCommandConsole.CommandConsole.Start () (at Assets/CommandConsole/Scripts/CommandConsole.cs:123)
I tried elevating the permissions of the Unity Editor, but it did not work.
I also tried putting Mono.CSharp.dll in the plugins folder, but that also did not work.
Any help is appreciated.

ServiceStackHost.Instance has already been set

I have a bizarre problem.
I have implemented a standard Service Stack API.
It has been working perfect for the last year now.
Last week I saw some major changes to Xamarin Studio so decided to update my IDE.
With that update NuGet comes standard, so I decided to convert my Service Stack Reference Files to use NuGet. Quite nice, now I can update the DLL's etc with 1 click.
So the API works fine on my Windows Machine now, no issues.
Problem: Our servers run Ubuntu with Mono installed. And it does not want to budge.
I Initially get this Error:
System.TypeLoadException: A type load exception has occurred.
at ServiceStack.VirtualPath.FileSystemVirtualPathProvider..ctor (IAppHost appHost, System.IO.DirectoryInfo rootDirInfo) [0x00000] in <filename unknown>:0
at ServiceStack.VirtualPath.FileSystemVirtualPathProvider..ctor (IAppHost appHost, System.String rootDirectoryPath) [0x00000] in <filename unknown>:0
at ServiceStack.ServiceStackHost.Init () [0x00000] in <filename unknown>:0
at API_NOTIFICATION.Global.Application_Start (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>: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, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Then I refresh the page and I keep getting this error:
System.IO.InvalidDataException: ServiceStackHost.Instance has already been set
at ServiceStack.ServiceStackHost.Init () [0x00000] in <filename unknown>:0
at API_NOTIFICATION.Global.Application_Start (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>: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, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Anyone Experienced this before? Anyone know how to fix this?
I did look at other posts similar to this one.
No idea where to find:
App_Start/ServiceStackFramework.cs
And no idea how to remove as again I cant find that anywhere. But who knows, it might not even be the same problem at all.
I have used the standard Tutorial to set up the API which is posted on the ServiceStack Website.
Any help will be much appreciated. And like I said, the only think that really changed is the IDE and updating those ServiceStack references via NuGet.
AppHost:
public class ResponseNotificationServiceAppHost : AppHostBase
{
public ResponseNotificationServiceAppHost() : base("Response Notification Services", typeof(NotificationServices).Assembly)
{
}
#region implemented abstract members of AppHostBase
public override void Configure(Funq.Container container)
{
}
#endregion
}
Global:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(Object sender, EventArgs e)
{
new ResponseNotificationServiceAppHost().Init();
}
}
API Service Class:
public class NotificationServices : Service
{
public PersonNotificationResponse Any(PersonNotification request)
{
// Implementation Code here
}
}
Update:
When trying to remove the AppHost, the API would not load. I get the following error on the Ubuntu Server:
System.NullReferenceException: Object reference not set to an instance of an object
at ServiceStack.HttpHandlerFactory..cctor () [0x00000] in <filename unknown>:0
Looks like it was a problem with the mono framework.
The server was running on 2.10.5 Mono on a 12.04 LTS server.
I hosted the API on a new 14.04 LST server with Mono 3.2.6 installed.
Works fine now.
Maybe ServiceStack was updated which was not compatible with 2.10.5 anymore. Not sure, can only speculate.

Xamarin Binding "Wrapper type '' is missing its native ObjectiveC Class

I am trying to bind an external accessory MSR SDK to Xamarin. Note, this is ON the device. It does not work in the simulator at all. I keep getting this error:
Wrapper type 'iMagProII.IMag' is missing its native ObjectiveC class 'IMag'.
Here is my .cs with my linker flags:
[assembly: LinkWith ("libiMagSDK.a", LinkTarget = LinkTarget.ArmV6 | LinkTarget.ArmV7 | LinkTarget.Simulator, SmartLink = true, ForceLoad = true, IsCxx = true, Frameworks = "CoreGraphics ExternalAccessory", LinkerFlags="-lsqlite3.0 -lc++")]
Here is the Stack Trace:
at MonoTouch.Registrar.DynamicRegistrar.OnRegisterType (MonoTouch.Registrar.ObjCType type) [0x00334] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pmcs-compat.DynamicRegistrar.cs:573
at MonoTouch.Registrar.Registrar.RegisterTypeUnsafe (System.Type type, System.Collections.Generic.List`1& exceptions) [0x009a4] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pmcs-compat.Registrar.cs:1027
at MonoTouch.Registrar.Registrar.RegisterType (System.Type type, System.Collections.Generic.List`1& exceptions) [0x00011] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pmcs-compat.Registrar.cs:680
at MonoTouch.Registrar.DynamicRegistrar.Register (System.Type type) [0x00002] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pmcs-compat.DynamicRegistrar.cs:839
at MonoTouch.ObjCRuntime.Class.Register (System.Type type) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pmcs-compat.Class.cs:126
at MonoTouch.ObjCRuntime.Class.GetHandle (System.Type type) [0x00000] in <filename unknown>:0
at MonoTouch.Foundation.NSObject.AllocIfNeeded () [0x00015] in /Developer/MonoTouch/Source/maccore/src/Foundation/.pmcs-compat.NSObject2.cs:390
at MonoTouch.Foundation.NSObject..ctor (MonoTouch.Foundation.NSObjectFlag x) [0x00006] in /Developer/MonoTouch/Source/maccore/src/Foundation/.pmcs-compat.NSObject2.cs:102
at iMagProII.IMag..ctor () [0x00000] in /Users//Projects/Xamarin/Bindings/Unimag/iMagProII/obj/Debug/ios/iMagProII/IMag.g.cs:112
at iMagProII.iOS.ImagViewController.ViewDidLoad () [0x0005c] in /Users//Projects/Xamarin/Bindings/Unimag/iMagProII.iOS/ImagViewController.cs:46
at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr)
at MonoTouch.UIKit.UIViewController.get_View () [0x00030] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIViewController.g.cs:2232
at iMagProII.iOS.ImagViewController..ctor () [0x00008] in /Users//Projects/Xamarin/Bindings/Unimag/iMagProII.iOS/ImagViewController.cs:23
at iMagProII.iOS.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00016] in /Users//Projects/Xamarin/Bindings/Unimag/iMagProII.iOS/AppDelegate.cs:33
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38
at iMagProII.iOS.Application.Main (System.String[] args) [0x00008] in /Users//Projects/Xamarin/Bindings/Unimag/iMagProII.iOS/Main.cs:17
Any help on getting this library bound would be greatly appreciated. I have been banging my head at it for hours now.
This occurs when the class IMag can't be found in the final executable.
The most common reasons for this are:
A typo in the class name.
The native library does not include the architecture you're building for. For instance the native library needs to contain i386 code to run in the simulator. You can use the lipo tool to list the architectures in the native library:
$ lipo -info /path/to/libiMagSDK.a
You also need to know the architecture you're using for the app, you can check this in the project's iOS Build settings, on the Advanced page. Select one that the native library includes.
The native library in the binding project depends on another native library which is not in the binding project (and the IMag class is in that other library). All non-platform dependencies must be included.
The native library is incorrectly built, and doesn't include the class you're trying to bind (this is not frequent, but I've still seen it several times). You can use the nm tool to check what's included in a native library:
$ nm -arch armv7 /path/to/libiMagSDK.a | grep IMag
0x0000f00d S _OBJC_CLASS_$_IMag
[other results]
The above result would indicate that the class is included in the native library.

Categories

Resources