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

Interception doesn't work for instance added with RegisterInstance #35

Open
vcpp opened this issue Mar 19, 2020 · 1 comment
Open

Interception doesn't work for instance added with RegisterInstance #35

vcpp opened this issue Mar 19, 2020 · 1 comment
Labels

Comments

@vcpp
Copy link

vcpp commented Mar 19, 2020

  1. Prepare some class and its interface.
  2. Manually create an instance of that class and register it using RegisterInstance method.
  3. Set transparent proxy interceptor for the interface.
  4. Resolve an object by the interface.

Current: container returns original instance, all method calls go directly to that instance.
Expected: container returns a proxy wrapper, method calls can be handled by the interceptor.

Broken versions: 5.x (tried 5.9.0 and latest stable 5.11.1).
Working versions: 4.0.1 and older.

There is no problem with RegisterType method, unity returns a proxy wrapper as expected in a such scenario. The issue affects only RegisterInstance-way. Both default and named instances are being returned unwrapped.

Below is a unit test to demonstrate the issue, it works well with Unity versions up to 4.0.1 and fails with 5.x:

[TestFixture]
public class InterceptionTests
{
    [Test]
    public void RegisteredInstanceIntercepting_ReturnsProxy()
    {
        var container = new UnityContainer();
        container.AddNewExtension<Interception>();

        var instance = new SomeService();

        container
            .RegisterInstance<ISomeService>(instance)
            .Configure<Interception>()
            .SetInterceptorFor<ISomeService>(new TransparentProxyInterceptor())
            .AddPolicy("SomePolicy")
            .AddMatchingRule(new TypeMatchingRule(typeof(ISomeService)))
            .AddCallHandler(new SomeCallHandler());

        var wrapper = container.Resolve<ISomeService>();

        Assert.IsTrue(RemotingServices.IsTransparentProxy(wrapper), "Wrapper is not a proxy");
        Assert.AreNotSame(instance, wrapper);
    }

    private interface ISomeService
    {
        void Foo();
    }

    private sealed class SomeService : ISomeService
    {
        public void Foo() => throw new NotImplementedException();
    }

    private sealed class SomeCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) => throw new NotImplementedException();
        public int Order { get; set; }
    }
}

Is it enough for you or should I attach a zip with the whole project?
Am I missing some breaking change with interceptors between 4.x and 5.x?
Is there some workaround?

@uniezuka
Copy link

uniezuka commented Apr 7, 2020

I have the same issue. Does anyone has already solve this?

@ENikS ENikS transferred this issue from unitycontainer/unity Apr 7, 2020
@ENikS ENikS added the Bug 🐛 label Apr 7, 2020
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

3 participants