Sample Interception program : JIT Compiler encountered an internal limitation.
Closed this issue · 2 comments
(Creating a new issue for easy tracking)
The following exception is being shown, while trying to run the sample Interception program:
"JIT Compiler encountered an internal limitation."
Please note that this exception is being thrown from within the following method (class BankAccount):
public void Deposit(int amount)
{
_balance += amount;
Console.WriteLine("Deposited: {0}", amount);
Console.WriteLine("New Balance: {0}", Balance);
}
So, if I comment the above method call (Deposit()) within the Pay() method, the program works fine:
public void Pay(int wage)
{
var currentAmount = wage;
//_checkingAccount.Deposit(currentAmount);
Console.WriteLine("Paid {0} units", wage);
}
However, my intention is to intercept all the methods and get return value correctly from within the target methods (If they are not void). For example:
Let's change the Pay() method to return an int
public int Pay(int wage)
{
var currentAmount = wage;
//_checkingAccount.Deposit(currentAmount);
Console.WriteLine("Paid {0} units", wage);
return wage;
}
And, Intercept the method call and also get the return value correctly from within the intercepted method as follows:
public object Intercept(IInvocationInfo info)
{
var methodName = info.TargetMethod.Name;
Console.WriteLine("method '{0}' called", methodName);
// Replace the input parameter with 42
var result = info.TargetMethod.Invoke(info.Target, new object[]{42});
//Get the return value correctly from the target method.
return result;
}
Right now, the program throws the mentioned exception and hence not able to return correct value.
Using a reflector, I took a look at the post weaven Pay() method, after modifying the Pay() as returning an int. There I found the following codes, alsong with the injected codes
... int amount = wage;
this._checkingAccount.Deposit(amount);
Console.WriteLine("Paid {0} units", wage);
int num2 = wage;
....
return (int) obj2;
//Please note that, the obj2 is never assigned with a value and hence, the postweaven methods always return null. This may give you some clue.
Thanks
Which commit is this from? Is it this one? http://github.com/philiplaureano/LinFu/commit/cb01d124a1433fc79d208951469064907d489737
I went ahead and wiped my local copy and pulled down a fresh copy of the LinFu repository onto my dev machine to make sure that I wasn't seeing things. I can't seem to replicate the issue, and the sample library is working fine even on my recently checked out copy...
I downloaded the latest source codes from GitHub. I tested the sample application in my Windows Vista, after converting both projects in the sample solution to .NET framework 4.0 and tested in VS 2010 (Because, I don't have framework 3.5 installed for VS 2010 in my laptop), and, found the above mentioned problems.
However, I tested the same codes in another PC (VS 2010, .NET framework 3.5 for "SampleLibrary" project and .NET framework 4.0 for the "LinFu.AOP.MethodInterceptionSample" (I had to convert the LinFu.AOP.MethodInterceptionSample" project to framework 4.0, otherwise, the solution wasn't building. This time, the interception works fine :)
I am not sure whether this is an issue related to the LinFu or my dev environment.
However, there is one very important issue. The intercepted method always returns null, even if the interception is working (This is used to work in the First version of LinFu). I am creating a separate issue on this.
Many thanks for your kind help and co-operation.