我自己写的拦截器无法通过在autofac中注册来拦截

时间:2020-05-17 11:11:14

标签: c# asp.net

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Register your own things directly with Autofac, like:
        builder.RegisterType<TransactionInterceptor>().AsSelf();
        builder.RegisterAssemblyTypes(Assembly.Load("MyTest.Service")).AsImplementedInterfaces()
            .EnableInterfaceInterceptors()
            .InterceptedBy(typeof(TransactionInterceptor));
        // builder.RegisterModule(new AutofacAnnotationModule(Assembly.Load("MatCloud")));
        builder.RegisterType<Controller>().PropertiesAutowired();

        // var assembly = this.GetType().GetTypeInfo().Assembly;
        // builder.RegisterType<TransactionInterceptor>();
        // builder.RegisterModule(new DefaultModule());
    }

但是在我运行方法时,它报了异常

 Autofac.Core.DependencyResolutionException: An exception was thrown while activating MyTest.Service.Services.Impl.OrderService.
       ---> System.InvalidCastException: Unable to cast object of type 'MyTest.Service.Aop.TransactionInterceptor' to type 'Castle.DynamicProxy.IAsyncInterceptor'.
         at System.Linq.Enumerable.CastIterator[TResult](IEnumerable source)+MoveNext()
         at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
         at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
         at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
         at Autofac.Annotation.RegistrationExtensions.<>c__DisplayClass8_0`3.<EnableInterfaceInterceptors>b__0(Object sender, ActivatingEventArgs`1 e)
         at Autofac.Core.Registration.ComponentRegistration.RaiseActivating(IComponentContext context, IEnumerable`1 parameters, Object& instance)
         at Autofac.Core.Resolving.InstanceLookup.CreateInstance(IEnumerable`1 parameters)
         --- End of inner exception stack trace ---

无法将类型为“ MyTest.Service.Aop.TransactionInterceptor”的对象转换为类型为“ Castle.DynamicProxy.IAsyncInterceptor”。 我使用过asp.net core3.1 我不知道该怎么办,谢谢

0 个答案:

没有答案
相关问题