Skip to content
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

System.ArgumentOutOfRangeException with VirtualMethodInterceptor and ref parameters #2

Open
ENikS opened this issue Oct 12, 2017 · 2 comments
Labels

Comments

@ENikS
Copy link
Contributor

ENikS commented Oct 12, 2017

@kostemar Wrote:

I ran into a problem with the Unity.Interception extension (using Unity v4.0.1 and Unity.Interception v4.0.1) and VirtualMethodInterception.

Since the issue is a bit difficult to explain, I attached a console application that reproduces the issue. Basically, I have some "domain objects" that look something like this:

public interface IDataImportJob
{
    void Run();
}

public abstract class DataImportBase : IDataImportJob
{
    public virtual void Run()
    {
        OnRun();
    }

    protected abstract bool OnRun();
}

public interface IProductPartAndOptionMasterDataImport : IDataImportJob
{
}

public class ProductPartAndOptionMasterDataImport : DataImportBase, IProductPartAndOptionMasterDataImport
{
    private readonly IOtherObject _otherObject;

    public ProductPartAndOptionMasterDataImport(IOtherObject otherObject)
    {
        _otherObject = otherObject;
    }

    protected override bool OnRun()
    {
        var productsOk = RunProductPartAndOptionImport();

        return productsOk;
    }

    protected virtual bool RunProductPartAndOptionImport()
    {
        int partsProcessed = 42;
        ImportProductPartFromProduct(ref partsProcessed);
        return true;
    }

    [TracingCallHandler(true, true, true)]
    // if you remove the "partsProcessed" parameter here, the issue does not occur.
    protected virtual void ImportProductPartFromProduct(ref int partsProcessed)
    {
        int someParameter = 0;
        _otherObject.SomeMethod(ref someParameter);
    }
}

public interface IOtherObject
{
    void SomeMethod(ref int someArgument);
}

public class OtherObject : IOtherObject
{
    public virtual void SomeMethod(ref int someArgument)
    {
        throw new Exception("Something bad happened.");
    }
}

The TracingCallHandler is a CallHandler that can log and swallow exceptions.

Now, when I run the console application, the "Something bad happened" exception is logged as expected, but then I get a second exception
(System.ArgumentOutOfRangeException) that crashes the application.

I guess the problem is that somewhere in the dynamically generated code (DynamicModule.ns.Wrapped_ ...) a ParameterCollection is
created with an empty arguments array. Then, the indexer of that ParameterCollection is used with an index of 0, which will eventually result in that System.ArgumentOutOfRangeException.

If you remove the partsProcessed parameter from the ImportProductPartFromProduct method, the issue does not occur.

@ENikS ENikS added the Bug 🐛 label Feb 5, 2018
@ENikS
Copy link
Contributor Author

ENikS commented Feb 5, 2018

I can reproduce it but have no clue how to fix. Will work on it...

@witskeeper
Copy link

get errors when i try to build the code
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants