-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DynamicProxy Throw NullReferenceException When Intercepted Method Throw Eaten Exception #85
Comments
I have looked at this a long time ago (BTW, this only affects value type return values and out parameters) but baking it into the framework would be quite tricky and add performance overhead that's unnecessary 99.99% of the time. A better option might be catching the But then, by all means, if you want to start a pull request and have a crack at it (preferably the second option though) I'm happy to help out |
Thanks for the clarification. I can't re-throw it as we do not want to have try-catch applied on intercepted method. It then become an unhandled exception. I'd like to help out here, do you mind give me some hint on which part of the code that I should look into? It seems that the exception is thrown at a dynamic generated proxy method which I am not familiar with the pattern yet. |
Sure. Can you start with some tests to demonstrate the issue and expected behaviour? |
Sure, I will add the code in Castle.Core.Tests. The workaround is updated for anyone who like to have a quick fix. |
I've added the test code under Castle.Core.Tests/BugsReported, However, the code fails to run, the error indicates that: Inheritance security rules violated while overriding member I am quite sure that previous Castle.Core.Tests does not have this problem. The issue happens on existing unit tests too, it seems none of them could run. Is there anything that I missed? Below is the code that I added as DynProxy85.cs:
|
what configuration are you building it with? Try .NET 4.0 or 4.5 configuration. Also please make sure there are no |
Thank you @kkozmic-seek , I didn't change any setting, the .NET configuration is the original project setting from repo which is .NET 4.5. I will remove the |
I think the problem is the ReSharper NUnit Plugin for Visual Studio 2013, there is no problem by using nunit directly. |
Cool. As for regions we avoid them altogether in our codebase. Resharper On Fri, 24 Apr 2015 17:27 Charles-521 notifications@github.com wrote:
|
IMHO, this is a user error, and not a fault with DynamicProxy: If you create a proxy without a target, then the interceptors are responsible for "implementing" the proxied methods' behaviour. In the above example, if the interceptor chooses to swallow any exception, then it's still responsible for "completing" the invocation. So it must either throw a new exception, or set the return value. Basically, the above code makes the same mistake as the following (only it surfaces at run-time instead of at compile-time): int GetSomeValue()
{
try
{
throw new InvalidOperationException();
}
catch
{
}
// compile-time error: method does not return a value.
} (Granted though, the type of exception thrown, |
@stakx after reading this thread again I agree, and just as both @kkozmic and yourself mentioned the exception could be much more helpful. I've submitted a pull request to improve the exception for return values, and also included if an interceptor plays with |
Just merged #364 that replaces the null ref exception with something more useful, it is still the job of an interceptor to set a return value if it wants to swallow exceptions. |
The issue will happen all of the below condition met:
We put our logging logic when the exception happens and since we do not intend to crash the application. Therefore such exception does not re-thrown.
Currently I can workaround this by putting below code in _finally block_:
However, it might be better to handle this in the framework itself.
The text was updated successfully, but these errors were encountered: