dongfo / autofac

Automatically exported from code.google.com/p/autofac
0 stars 0 forks source link

AutofacContrib.DynamicProxy2: Problem with interception #359

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

I tried to use the interception from Autofac but I keep getting the following 
exception:

Autofac.Core.DependencyResolutionException: An exception was thrown while 
executing a resolve operation. See the InnerException for details. ---> 
System.MethodAccessException: Attempt by method 
'AutofacContrib.DynamicProxy2.RegistrationExtensions.<EnableInterfaceInterceptor
s>b__7(System.Object, Autofac.Core.ActivatingEventArgs`1<System.Object>)' to 
access method 
'System.Linq.Enumerable.Cast<Castle.DynamicProxy.IInterceptor>(System.Collection
s.IEnumerable)' failed.

Can any one give me a hint? Did I miss anything?

Thanks a lot.

My code is as follow:

using Castle.DynamicProxy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Autofac;
using AutofacContrib.DynamicProxy2;

namespace AutofacContrib.Tests.DynamicProxy2
{
    [TestClass]
    public class InterceptorsChosenByMetadataFixture
    {
        public interface ICustomerService
        {
            int GetVisitCount();
        }

        public class CustomerService : ICustomerService
        {
            int VisitCount { get; set; }

            public CustomerService()
            {
                VisitCount = 10;
            }

            public int GetVisitCount()
            {
                return VisitCount;
            }
        }

        class AddOneInterceptor : IInterceptor
        {
            public void Intercept(IInvocation invocation)
            {
                invocation.Proceed();
                if (invocation.Method.Name.StartsWith("Get"))
                    invocation.ReturnValue = 1 + (int)invocation.ReturnValue;
            }
        }

        [TestMethod]
        public void InterceptsWhenUsingExtendedPropertyAndType()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<CustomerService>()
                .As<ICustomerService>()
                .EnableInterfaceInterceptors()
                .InterceptedBy(typeof(AddOneInterceptor));
            builder.RegisterType<AddOneInterceptor>();
            var container = builder.Build();
            var cs = container.Resolve<ICustomerService>();
            Assert.AreEqual(11, cs.GetVisitCount());
        }
    }
}

Original issue reported on code.google.com by tigerhao...@gmail.com on 16 Apr 2012 at 1:52

Attachments:

GoogleCodeExporter commented 8 years ago
Im having the same problem just now trying to get interceptors to work. Did you 
manage to solve this?

Original comment by mathias....@gmail.com on 18 Apr 2012 at 7:14

GoogleCodeExporter commented 8 years ago
Hi guys... I've no actual solution for the problem, just wanted to let you know 
that if you reference the previous Autofac version (2.5.2.0) then interception 
works correctly. You might want to use the older version until this gets 
resolved...

Thanks,
Kostas

Original comment by Karampel...@gmail.com on 23 Apr 2012 at 10:52

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 21 Sep 2012 at 4:23

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 21 Sep 2012 at 4:34

GoogleCodeExporter commented 8 years ago
It appears we have this exact test fixture in the 
Autofac.Extras.Tests.DynamicProxy2 unit test assembly and it is currently 
passing in the 3.0 branch. Since it doesn't appear we changed any code, I'm 
guessing it has to do with us updating to the latest Castle version.

Anyway, this appears to be fixed, so I'm marking it fixed. If it's not working 
for you, make sure you're on the latest 3.0 version of Autofac and verify.

Original comment by travis.illig on 14 Dec 2012 at 12:19

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 14 Dec 2012 at 12:19