Asp.Net Core Dependency Injection doesn't work, if it is started from unmanaged/native - c++

First i try to explain the story.
I wanted to extend an C++/MFC application with REST-APIs, and decided to use for this purpose Asp.Net-Core 5 in a library by bridging it to unmanaged code with C++/CLI library. (having a separete ASP application is doubled expenditure, and needed to be rewritten all the logic in C#. in that regard RESTful-Server should be in same process implemented.)
Asp-Host is started in that way; C++/MFC -> C++/CLI -> ASP-Library (ASP referenced in CLI, CLI referenced in Native)
First problem was; by building the host Microsoft.Extensions.Hosting.Abstractions-Assembly could not be resolved. I found that, "My CLI Library".runtimeconfig.json has wrong framework reference and causes it not to be resolved. That means generated runtimeconfig.json is wrong. After every build it has to be manually with AspNetCore instead of NETCore corrected. (Done in PostBuildEvent)
Next problem; during the ASP-Host build, ASP could not resolve "My ASP Library.dll"-Assembly (in reflection). This problem solved by OnAssemblyResolve event by giving the right path. I'm not sure whether it is correct solution. Because AppDomain.BaseDirectory is an empty string, maybe it is the cause of it, that the library could not found.
In AssemblyResolve event;
Assembly::LoadFrom(assemblyFile)
Finally i could start the server, and it works. Then needed to use dependency injection, and my service could not be resolved from the controller.
Then i used my ASP-Library in another C#-project to test and it works...
i'm sure that it doesn't work if the entry point of process is in unmanaged code.
public interface IServerImpl
{
void OnFail(String msg);
}
public class CServerImpl : IServerImpl
{
public void OnFail(String msg)
{
}
}
... in Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IServerImpl, CServerImpl>();
services.AddControllers();
}
... Controller
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IServerImpl _serverImpl;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IServerImpl serverImpl)
{
_logger = logger;
_serverImpl = serverImpl;
}
...
}
Is there any workaround to have it working? Are those problems bug in Asp.Net-Core or what am i doing wrong?
Thanks in advance

The problem has been solved. Assembly resolving event with Assembly.LoadFrom() causes my ASP-assembly to be loaded (or mapped) twice. Loading it in different assembly-load-context means that, doubled Types, static variables and so on. In that regard my registered IServerImpl service searched in the controller with another IServerImpl type. Type names are identical, but not the same.
For more information see technical challenges: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext?view=net-5.0
Another issue on GitHub: https://github.com/dotnet/runtime/issues/39783
In assembly resolve event, i'm returning now the loaded assembly like that, instead of loading it again;
AppDomain.CurrentDomain.GetAssemblies()
.SingleOrDefault(asm => asm.FullName.StartsWith(args.Name.Split(',')[0] + ","))
still wellcome answers which clarifies "why my ASP-assembly not found from the framework?"

Related

Override System class in Java and more precisely currentTimeMillis [duplicate]

Aside from recompiling rt.jar is there any way I can replace the currentTimeMillis() call with one of my own?
1# The right way to do it is use a Clock object and abstract time.
I know it but we'll be running code developed by an endless number of developers that have not implemented Clock or have made an implementation of their own.
2# Use a mock tool like JMockit to mock that class.
Even though that only works with Hotspot disabled -Xint and we have success using the code bellow it does not "persist" on external libraries. Meaning that you'd have to Mock it everywhere which, as the code is out of our control, is not feasible. All code under main() does return 0 milis (as from the example) but a new DateTime() will return the actual system millis.
#MockClass(realClass = System.class)
public class SystemMock extends MockUp<System> {
// returns 1970-01-01
#Mock public static long currentTimeMillis() { return 0; }
}
3# Re-declare System on start up by using -Xbootclasspath/p (edited)
While possible, and though you can create/alter methods, the one in question is declared as public static native long currentTimeMillis();. You cannot change it's declaration without digging into Sun's proprietary and native code which would make this an exercise of reverse engineering and hardly a stable approach.
All recent SUN JVM crash with the following error:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000, pid=4668, tid=5736
4# Use a custom ClassLoader (new test as suggested on the comments)
While trivial to replace the system CL using -Djava.system.class.loader JVM actually loads up the custom classLoader resorting to the default classLoader and System is not even pushed trough the custom CL.
public class SimpleClassLoader extends ClassLoader {
public SimpleClassLoader(ClassLoader classLoader) {
super(classLoader);
}
#Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
}
We can see that java.lang.System is loaded from rt.jar using java -verbose:class
Line 15: [Loaded java.lang.System from C:\jdk1.7.0_25\jre\lib\rt.jar]
I'm running out of options.
Is there some approach I'm missing?
You could use an AspectJ compiler/weaver to compile/weave the problematic user code, replacing the calls to java.lang.System.currentTimeMillis() with your own code. The following aspect will just do that:
public aspect CurrentTimeInMillisMethodCallChanger {
long around():
call(public static native long java.lang.System.currentTimeMillis())
&& within(user.code.base.pckg.*) {
return 0; //provide your own implementation returning a long
}
}
I'm not 100% sure if I oversee something here, but you can create your own System class like this:
public static class System {
static PrintStream err = System.err;
static InputStream in = System.in;
static PrintStream out = System.out;
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) {
System.arraycopy(src, srcPos, dest, destPos, length);
}
// ... and so on with all methods (currently 26) except `currentTimeMillis()`
static long currentTimeMillis() {
return 4711L; // Your application specific clock value
}
}
than import your own System class in every java file. Reorganize imports in Eclipse should do the trick.
And than all java files should use your applicatikon specific System class.
As I said, not a nice solution because you will need to maintain your System class whenever Java changes the original one. Also you must make sure, that always your class is used.
As discussed in the comments, it is possible that option #3 in the original question has actually worked, successfully replacing the default System class.
If that is true, then application code which calls currentTimeMillis() will be calling the replacement, as expected.
Perhaps unexpectedly, core classes like java.util.Timer would also get the replacement!
If all of the above are true, then the root cause of the crash could be the successful replacement of the System class.
To test, you could instead replace System with a copy that is functionally identical to the original to see if the crashes disappear.
Unfortunately, if this answer turns out to be correct, it would seem that we have a new question. :) It might go like this:
"How do you provide an altered System.currentTimeMillis() to application classes, but leave the default implementation in place for core classes?"
i've tried using javassist to remove the native currentTimeMills, add a pure java one and load it using bootclasspath/p, but i got the same exception access violation as you did. i believe that's probably because of the native method registerNatives that's called in the static block but it's really too much to disassemble the native library.
so, instead of changing the System.currentTimeMills, how about changing the user code? if the user code already compiled (you don't have source code), we can use tools like findbugs to identify the use of currentTimeMillis and reject the code (maybe we can even replace the call to currentTimeMills with your own implementation).

DryIOC Container Contents

StructureMap has a super-useful debug method on the Container class called WhatDoIHave()
It shows every type in the container along with its lifecycle, guid and a bunch of other info. It's useful for debugging.
There's some info here:
http://jeremydmiller.com/2014/02/18/structuremap-3-is-gonna-tell-you-whats-wrong-and-where-it-hurts/
Does DryIOC have an equivalent debug feature?
(I'm the creator of DryIoc).
You can use container.GetServiceRegistrations() to get registration infos as #fyodor-soikin suggested.
But at the latest version (2.0.0-rc3build339) I have added VerifyResolutions method that may help you to diagnose potential resolution problems, including missing registrations as well. Here the wiki explaining it.
Example from the wiki:
// Given following SUT
public class RequiredDependency {}
public class MyService { public MyService(RequiredDependency d) {} }
// Let's assume we forgot to register RequiredDependency
var container = new Container();
container.Register<MyService>();
// Find what's missing
var results = container.VerifyResolutions();
Assert.AreEqual(1, results.Length);
Verify result is array of ServiceRegistrationInfo and ContainerException KeyValuePairs. In this example the registration info will be:
MyNamespace.MyService registered as factory {ID=14, ImplType=MyNamespace.MyService}
And the exception will be:
DryIoc.ContainerException: Unable to resolve MyNamespace.RequiredDependency as parameter "d"
in MyNamespace.MyService.
Where no service registrations found
and number of Rules.FallbackContainers: 0
and number of Rules.UnknownServiceResolvers: 0
Update:
The functionality is available in latest stable DryIoc 2.0.0.

Calling a C++ dll method from Java using JNA and avoiding Method Name Mangling

I have been going through links on StackOverflow on how to resolve the Method name mangling but did not find any solution with a real time example.
Scenario-A C++ Ex.dll file is provided by client. I need to access the Ex.dll and call the methods in the same through Java.
Restrictions- Cannot modify the Ex.dll, i can only access the same.
Issue Faced- Getting the below exception when i access the Ex.dll through JNA
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'getCPUSpeed': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:134)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:336)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:316)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at $Proxy0.getCPUSpeed(Unknown Source)
at cpp.java.JnaTest.main(JnaTest.java:16)
Googled a lot and found that its due to method name Mangling, but again could not find any good sample code with the solution.
This is code i used-
import com.sun.jna.Native;
class JnaTest
{
public static void main(String args[])
{
try
{
JnaInterface jInterface = (JnaInterface) Native.loadLibrary("Ex", JnaInterface.class);
System.out.println("Calling C++ DLL method");
System.out.println("========================");
System.out.println("getCPUSpeed() -- "+jInterface.getCPUSpeed());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cpp.java;
import com.sun.jna.Library;
public interface JnaInterface extends Library{
public int getCPUSpeed();
}
Update 1: **************************************
Below mentioned is the actual functions i get when i browse the DBMM.dll through dependency walker-
DBMM DLL functions-
??0cDbmmInterfaceCache##QAE#ABV0##Z
??0cDbmmInterfaceCache##QAE#XZ
??0cDbmmInterfaceControl##QAE#ABV0##Z
??0cDbmmInterfaceControl##QAE#XZ
??0cDbmmInterfaceEcon##QAE#ABV0##Z
??0cDbmmInterfaceEcon##QAE#XZ
??0cDbmmInterfaceKnob##QAE#XZ
??0cDbmmInterfaceOutput##QAE#ABV0##Z
??0cDbmmInterfaceOutput##QAE#H#Z
??0cDbmmInterfacePoolLoan##QAE#ABV0##Z
??0cDbmmInterfacePoolLoan##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z
??0cDbmmMacroEcon##QAE#ABV0##Z
??0cDbmmMacroEcon##QAE#ABVcDbmmInterfaceEcon##_N#Z
??0cDbmmMtgBasisConstSpreadModel##IAE#XZ
??0cDbmmMtgBasisConstSpreadModel##QAE#ABV0##Z
??0cDbmmMtgBasisConstSpreadModel##QAE#PBD#Z
??0cDbmmMtgBasisModel##QAE#ABV0##Z
??0cDbmmMtgBasisModel##QAE#XZ
??0cScaleFieldsSubSum##QAE#NN#Z
??1cDbmmInterfaceCache##QAE#XZ
??1cDbmmInterfaceControl##QAE#XZ
??1cDbmmInterfaceEcon##QAE#XZ
??1cDbmmInterfaceKnob##QAE#XZ
??1cDbmmInterfaceOutput##QAE#XZ
??1cDbmmInterfacePoolLoan##QAE#XZ
??1cDbmmMacroEcon##QAE#XZ
??1cDbmmMtgBasisConstSpreadModel##UAE#XZ
??1cDbmmMtgBasisModel##UAE#XZ
??1cScaleFieldsSubSum##QAE#XZ
??4cDbmmInterface##QAEAAV0#ABV0##Z
??4cDbmmInterfaceCache##QAEAAV0#ABV0##Z
??4cDbmmInterfaceControl##QAEAAV0#ABV0##Z
??4cDbmmInterfaceEcon##QAEAAV0#ABV0##Z
??4cDbmmInterfaceKnob##QAEAAV0#ABV0##Z
??4cDbmmInterfaceOutput##QAEAAV0#ABV0##Z
??4cDbmmInterfacePoolLoan##QAEAAV0#ABV0##Z
??4cDbmmMacroEcon##QAEAAV0#ABV0##Z
??4cDbmmMtgBasisConstSpreadModel##QAEAAV0#ABV0##Z
??4cDbmmMtgBasisModel##QAEAAV0#ABV0##Z
??4cScaleFieldsSubSum##QAEAAV0#ABV0##Z
??_7cDbmmMtgBasisConstSpreadModel##6B#
??_7cDbmmMtgBasisModel##6B#
??_FcDbmmInterfaceOutput##QAEXXZ
??_FcDbmmInterfacePoolLoan##QAEXXZ
??_FcScaleFieldsSubSum##QAEXXZ
?Add#cScaleFieldsSubSum##QAEXNN#Z
?InitSubsum#cScaleFieldsSubSum##QAEXNN#Z
?ReInit#cDbmmMacroEcon##QAEX_N#Z
Not sure how can i call these functions through Java.
Appreciate if somebody could provide me a solution from Java end, with a sample code please :)
Your function is decorated with JNI and stdcall conventions; it is not C++-mangled.
It looks like the library is a JNI library, given the Java_sysInfo_ prefix. If that is the case, you need only declare the equivalent Java-side, e.g.
// default package
public class sysInfo {
static { System.loadLibrary("Ex"); }
public static native int getCPUSpeed();
}
I think you'll probably find that this mapping is the correct one and that you don't need JNA.
EDIT
Given a C++ class with an arbitrary ctor input argument and method getCount():
extern "C" int getCountForName(const char* name) {
MyCPPClass mycpp(name);
return mycpp.getCount();
}
Compile that into a shared library, and load via JNA.
If your building the DLL through Visual studio, you should be able to build it as a release instead of debug to fix the function names if you are not already doing this.

What is a good way to get a C++ and WPF app to communicate

I have a C++ dll which is a plug-in to ADOBE Acrobat. It needs to talk often ( to and fro) and with a fair amount of complicated data-structures to a WPF process.
Any thoughts of what might be the best way to go . Need something that is a little long term and maintainable, in other words would love some ideas around something that lets both process make what looks like methods calls and some infrastructure piece does the marshaling and dispatch . I've tried Windows messages but ran into some conflict issues on ADOBE, also not really interested in anything that causes the dll to get adobe to load the CLR. Only other things that come to my mind are named pipes or http.
Thanks in Advance
Named pipes could do but you won't get a feeling of just calling functions. Named pipe are quite low-level IPC. Other IPC options are:
Windows RPC, you definitely get a feeling of just calling functions.
What about hosting a COM object in WPF application and calling it from the Adobe plugin?
I would go with COM:
Implement an interface in WPF app
generate a typelib (e.g by using regasm)
import the typelib into C++ dll
communicate
if you need bidirectional communication, C++ dll can also implement a COM interface which is then accessed from WPF application.
This is what I have used to connect legacy C++ app with new .NET service, and it works great. The biggest issue is to find people who know COM, but fortunately this doesn't require a deep understanding of COM.
your hint with COM is very intersting. I tried to implement this concept.
I have created an interface in my WPF CallDllFromWpf3Interface project:
using System.Runtime.InteropServices;
namespace CallDllFromWpf3Interface
{
[Guid("F6E0E2E8-CCC6-487B-8BF1-261265061E6A")]
public interface SetValueInterface
{
void SetValue(int value);
}
}
Then I have generated the typelib with the regasm tool:
regasm CallDllFromWpf3Interface.exe /tlb
With the "oleview" tool I can see the typelib and the interface.
The next step was to create a c++ dll project called "CallSetValueInterface".
In my CallSetValueInterface.cpp file I wrote this lines:
#import "D:\Thomas\Programming\WPF\Basics\CallDllFromWpf\CallDllFromWpf3Interface\CallDllFromWpf3Interface\bin\Debug\CallDllFromWpf3Interface.tlb"
void
CallSetValueInterface::startAcq(void)
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
CallDllFromWpf3Interface::SetValueInterfacePtr Svip("f6e0e2e8-ccc6-487b-8bf1-261265061e6a");
Svip->SetValue(55);
Svip = NULL;
CoUninitialize();
}
After a successful build of the dll project I copied "CallSetValueInterface.dll" to the "CallDllFromWpf3Interface" project.
Finally I changed my WPF code to:
#region SetValueInterface Members
public void SetValue(int value)
{
MyValue = value;
}
#endregion
[DllImport("CallSetValueInterface.dll", EntryPoint = "startAcq", ExactSpelling = true, SetLastError = true)]
public static extern void StartAcqFromDll();
private void Button_Click(object sender, RoutedEventArgs e)
{
StartAcqFromDll();
}
And when the debugger came to StartAcqFromDll() there occurred an error dialog "An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in CallDllFromWpf3Interface.exe".
Does anybody know whats going wrong?
Regards,
ThomasL.

Firebreath how to know if my method is called without debugging

I am using Objective-C++ in my firebreath project. The problem is that I am using Xcode 4 and I can not find the way to debug my project. So I have thought about if my method is been called from the web page.
Here is my source code:
In my OpenOnDesktopPluginAPI.h class:
class OpenOnDesktopPluginAPI : public FB::JSAPIAuto
{
public:
OpenOnDesktopPluginAPI(const OpenOnDesktopPluginPtr& plugin, const FB::BrowserHostPtr& host);
virtual ~OpenOnDesktopPluginAPI();
OpenOnDesktopPluginPtr getPlugin();
...
//This is my method
void runNotification();
...
};
In my OpenOnDesktopPluginAPI.mm class:
OpenOnDesktopPluginAPI::OpenOnDesktopPluginAPI(const OpenOnDesktopPluginPtr& plugin, const FB::BrowserHostPtr& host) : m_plugin(plugin), m_host(host)
{
...
//Register my method
registerMethod("runNotification", make_method(this, &OpenOnDesktopPluginAPI::runNotification));
...
}
//DistributedNotification class is my objective-c class with the implementation for post a distributed notification.
void OpenOnDesktopPluginAPI::runNotification()
{
DistributedNotification * notificationClass = [[DistributedNotification alloc] init];
[notificationClass postNotification];
[notificationClass release];
}
In my FBControl.html:
...
function myFunction()
{
plugin().runNotification();
}
...
My new method
...
I put my DistributedNotification.mm class in the
Build Phases -> "Compile Sources"
for my plugin target.
But I donĀ“t know if my runNotification method is called, because when (In my web page) I click on My new method link, nothing happens.
I'll repeat what I said on the forum when you ask; perhaps you haven't seen that answer yet:
First of all, you can debug with Xcode4, at least on some browsers; the trick is figuring out which process to connect to.
Secondly, you can always use NSLog to log things to the console. Thirdly, you could use log4cplus (see http://www.firebreath.org/display/documentation/Logging).
Finally, you haven't specified what browser you're testing on, nor have you indicated what happens. It looks reasonable, but aparently doesn't work? What doesn't work? What does it do?
It's nearly impossible to give you any useful advice without detailed information about what you are encountering.