How to use library which include Xamarin.iOS in Unity? - c#

I created dll which include Xamarin.iOS by Visual Studio 2017.
using JavaScriptCore;
using Foundation;
namespace JSCore
{
public static class MyClass
{
public static int Test()
{
JSContext jsContext = new JSContext();
jsContext[new NSString("arg1")] = JSValue.From(2, jsContext);
jsContext[new NSString("arg2")] = JSValue.From(2, jsContext);
var jsResult = jsContext.EvaluateScript("arg1 + arg2;");
return jsResult.ToInt32();
}
}
}
I want to use this library in Unity.
I put this dll in Unity Plugins folder and write script (call MyClass.Test()).
In Editor, there is no error. But when I build project, some error appeared.
IL2CPP error for method 'System.Void ObjCRuntime.Runtime::set_UseAutoreleasePoolInThreadPool(System.Boolean)' in assembly '/Users/[MyName]/Documents/JavaScriptRuntimeTest/Temp/StagingArea/Data/Managed/Xamarin.iOS.dll'
Additional information: Build a development build for more information. Object reference not set to an instance of an object.
I read error sentence. I think this is the version error about System.dll.
Xamarin.iOS reference System.dll 2.0.5.0.
Unity's API Compatibility Level is .NET Standard 2.0.
How to avoid this error ?

Unity's Mono runtime isn't compatible with Xamarin, so the only possibility to use your library is to remove the IOS part.

Related

Does SevenZipSharp work with .NETCore on Windows?

I have a .NETCore app which I am trying to add 7 zip functionality to.
Compiling gives this warning:
warning NU1701: Package 'SevenZipSharp 0.64.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project.
So I presume the project is .NETCore v5.0. Can I run SevenZipSharp in this project?
Running the app gives an error at the call to CompressFiles: SevenZip.SevenZipLibraryException: 'Can not load 7-zip library or internal COM error! Message: failed to load library.'
public void ZipQOB(string sevenZipDllPath, string zippedQobPath, string unzippedQobFiles)//List<string> sourceFiles)
{
// throw exception if paths passed in are null, does 7zipsharp throw exceptions in this case?
try
{
if (System.IO.File.Exists(sevenZipDllPath) && System.IO.Directory.Exists(zippedQobPath))// && System.IO.Directory.Exists(unzippedQOBFiles))
{
string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "7z.dll");
//SevenZipCompressor.SetLibraryPath(sevenZipDllPath);
SevenZipCompressor.SetLibraryPath(path);
SevenZipCompressor sevenZipCompressor = new()
{
CompressionLevel = SevenZip.CompressionLevel.Ultra,
CompressionMethod = CompressionMethod.Lzma
};
string[] files = System.IO.Directory.GetFiles(unzippedQobFiles);
sevenZipCompressor.CompressFiles(zippedQobPath + #"\zip.QOB", files);
//System.IO.Path.ChangeExtension(zippedQobPath, ".QOB");
}
This question How do I use 7zip in a .NET Core app running on Linux? mentions a CLI wrapper ported from .NET Framework to .NET Core, but I can't find any details - is this something I would have to write and how?
I have already tried things suggested elsewhere, I altered the project build setting to:
Platform Target = AnyCPU,
ticked Prefer 32-bit
Should I just look at a different option as this page seems lists some stating .netcore compatible: https://github.com/topics/7zip?l=c%23
Many thanks for any help :)

Error on SSIS Script Task to load data from MongoDB to SQL Server: Could not load file or assembly MongoDB.Driver.Core

I want to create a Script Task to load data from MongoDB to SQL Server.
I am using Visual Studio 2017 and target SQL Server version is 2017.
The default target .NET Version of the script task based on the above is 4.5, therefore I got the 2.3 version, which was the latest to target 4.5
In order to get the dlls, I first used NuGet to download the packages and then copied the following dlls:
MongoDB.Driver.dll
MongoDB.Driver.Core.dll
MongoDB.Bson.dll
I followed instructions from here:
http://codeingaddiction.blogspot.com/2011/06/how-to-add-strong-name-to-existing-dll_16.html
In order to produce strong named dlls.
Finally, I added the dlls on GAC on the following path
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
I then created a script task and manually added the 3 dlls.
My test code is the following:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Bson.Serialization;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
}
public override void CreateNewOutputRows()
{
MongoClient client = new MongoClient("mongodb://dgm-mongodbdev-01.development.dc.opap:27017");
IMongoDatabase database = client.GetDatabase("sportsbetting-staging");
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("event");
collection.Find<BsonDocument>(null).Limit(3);
Console.WriteLine(collection);
}
}
Trying to build gives the following error:
Severity Code Description Project File Line Suppression State
Error The type 'IAsyncCursorSource<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'MongoDB.Driver.Core, Version=2.3.0.157, Culture=neutral, PublicKeyToken=null'.
MongoDb Core Driver does not have any additional dependencies that I may have missed. For 4.5 version of .NET the only dependency is MongoDB.Bson which is added as a reference.
The only article I have found for SSIS script task is this:
https://arcanecode.com/2014/01/14/importing-mongodb-data-using-ssis-2012/
Which is obsolete because it used the version 1.x of the driver.
Any ideas? The message seems misleading because it seems clear that I am not missing any dependency.
Finally, my answer was provided on the following post:
How to fix "Referenced assembly does not have a strong name" error?
Apparently,
MongoDB.Driver.Core.dll
MongoDB.Driver.dll
Have reference to
MongoDB.Bson.dll
Therefore, the public key token had to be added on the il files before re-assembling

How to fix "libtensorflow DllNotFoundException" when I already have the file?

I'm new to C# and I've been researching this error for a long time.
I wanted to use my trained TensorFlow model with Tensorflowsharp in Visual Studio for Mac so I created a .NET console project and installed TensorflowSharp 1.13.0 from nuget.org using the built-in package manager.
However, when I included the following code in Program.cs
using System;
using TensorFlow;
namespace ai
{
class MainClass
{
public static void Main(string[] args)
{
var graph = new TFGraph();
}
}
}
I got this error message:
Unhandled Exception:
System.DllNotFoundException: libtensorflow
at (wrapper managed-to-native) TensorFlow.TFGraph.TF_NewGraph()
at TensorFlow.TFGraph..ctor () [0x00022] in <a8cd6d02fde04a81817b4f25d24a7be9>:0
According to some of the posts that I found, this might states that libtensorflow.dylib is not in bin/Debug/ directory, but when I looked into it I found the file inside.
What am I doing wrong?
My environment setup:
Mac OSX 10.14
Visual Studio for Mac COMMUNITY 7.7.4(build 1)
Target framework: .NET 4.7.1
Thank you so much for any help!
Try change to 64 bit.
This may not correct answer to you. But give a try.
I dont have mac environment. But I tried woth .net core and TensorflowSharp and it works fine.
Same time I tried with .net framework and I also got dll not found issue and BadImageFormatException.
https://github.com/migueldeicaza/TensorFlowSharp/issues/103
https://github.com/migueldeicaza/TensorFlowSharp/issues/103

Call python script from .Net Core using pythonnet

I'm trying to get pythonnet to work in my .Net Core app running on Linux.
I've made a reference to Python.Runtime.dll (which I got from nuget) in my .Net Core project.
My code is:
using System;
using Python.Runtime;
namespace pythonnet_w
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start");
using (**Py.GIL()**) {
// blabla
}
Console.WriteLine("End");
}
}
}
I get this runtime error:
Unhandled Exception: System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder
System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
at Python.Runtime.CodeGenerator..ctor()
at Python.Runtime.DelegateManager..ctor()
at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize()
at Python.Runtime.Py.GIL()
at pythonnet_w.Program.Main(String[] args) in D:\Development\~.Net libraries (3.part)\phytonnet\.Net Core test (phytonnet)\c#\pythonnet_test\Program.cs:line 10
/usr/sbin/pythonnet_w: line 5: 19487 Aborted dotnet "/usr/share/pythonnet_wit/pythonnet_w.dll"
Tried to find a solution in these threads but without any luck:
How do I run a py file in C#?
Call Python from .NET
UPDATE:
I tried to open \pythonnet-master\src\runtime**Python.Runtime.csproj** in Visual Studio to see if I can compile it to .Net or .Core, but I can only compile to .Net framework.
I found this article "How to port from .net framework to .net standard"
Is that what I have to do?
I finally had success by using a self-compiled Python.Runtime.dll as of version 2.4.0. There are two options to create a working DLL:
Remove the other target framework net461 from the respective project file (leaving only netstandard2.0).
Run dotnet build using the appropriate options
For option 2, the following works(in Windows, Mac and Linux):
Clone the pythonnet repo (https://github.com/pythonnet/pythonnet)
In the pythonnet folder, cd src\runtime
Run dotnet build -c ReleaseWinPY3 -f netstandard2.0 Python.Runtime.15.csproj in Windows(in Mac/Linux, replace ReleaseWinPY3 with ReleaseMonoPY3 because the former use python37 and the later use python3.7)
Set DYLD_LIBRARY_PATH in Mac or LD_LIBRARY_PATH in linux(Windows skip):
export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.7/lib
Use the built DLL bin\netstandard2.0\Python.Runtime.dll as DLL reference in your Visual Studio .NET Core project (mine targets netcoreapp2.2, netcoreapp3.1 is also tested ok), e.g. in conjunction with the following code,
using System;
using Python.Runtime;
namespace Python_CSharp
{
class Program
{
static void Main(string[] args)
{
using (Py.GIL())
{
dynamic os = Py.Import("os");
dynamic dir = os.listdir();
Console.WriteLine(dir);
foreach (var d in dir)
{
Console.WriteLine(d);
}
}
}
}
}
You can host the IronPython interpreter right in your .NET application. For example, using NuGet, you can download the right package and then embed the script execution (actually the IronPython engine) right into your application.
Ref:
https://medium.com/better-programming/running-python-script-from-c-and-working-with-the-results-843e68d230e5

target .net framework class library with System.ServiceModel dependency from a .net core application

It should be possible to reference .net framework class libraries from .net core 2 projects in Visual Studio 2017 but I get a runtime exception when trying reference a class library dependent System.ServiceModel.
Create a “Console App (.NET Core)”. Visual studio set target framework to ".NET Core 2.0".
Create a “Class Library (.Net Framework)”. Visual studio set target framework to ".NET Framework 4.6.1".
Reference “System.ServiceModel 4.0.0.0” from the class library.
Fill Class1 in the class library with the following:
public class Class1
{
public void Test()
{
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("");
}
}
Reference the class library from the console app.
From the main method of the console app call the test method in Class1:
class Program
{
static void Main(string[] args)
{
new Class1().Test();
}
}
Build and run. An exception is thrown then the Test() method is tried executed:
System.IO.FileNotFoundException occurred HResult=0x80070002
Message=Could not load file or assembly 'System.ServiceModel,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
The system cannot find the file specified. Source= StackTrace: at ClassLibrary1.Class1.Test()
in c:\users\rth\source\repos\ConsoleApp2\ClassLibrary1\Class1.cs:line
15 at ConsoleApp1.Program.Main(String[] args) in
c:\users\rth\source\repos\ConsoleApp2\ConsoleApp1\Program.cs:line 11
System.ServiceModel is not in the /bin/Debug folder of either project.
I have tried to manually copy the System.ServiceModel to the /bin/debug folder of the console app but get the same error.
How do I reference a class library that reference System.ServiceModel without getting runtime exceptions?
To make the code above working without the exception:
Update your Class Library (.Net Framework) to Class Library (.NET Standard 2.0)
Add NuGet package System.ServiceModel.Http (version v4.4.0) to Class Library (.NET Standard 2.0) dependencies.

Categories

Resources