Autofac.Extras.DynamicProxy public 界面出错

Autofac.Extras.DynamicProxy got error with public interface

我有public接口和内部实现,

public interface IService
{
    ...
}
internal class Service: IService
{
    ...
}

我通过

注册了他们
builder.RegisterAssemblyTypes(assembly)
       .EnableInterfaceInterceptors()
       .AsImplementedInterfaces()
       .AsSelf()

但是我遇到了错误

The component Activator = Service (ReflectionActivator), Services = [~.IService, ~.Service], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope cannot use interface interception as it provides services that are not publicly visible interfaces. Check your registration of the component to ensure you're not enabling interception and registering it as an internal/private interface type.

为什么会出现此错误?我的界面是public.

使用AsSelf()方法时,容器会在当前注册的服务列表中添加具体类型。

错误信息是

cannot use interface interception as it provides services that are not publicly visible interface

也就是说注册的所有服务都应该是一个接口,不是这样的。您可以在 EnsureInterfaceInterceptionApplies method

中看到它
private static void EnsureInterfaceInterceptionApplies(IComponentRegistration componentRegistration)
{
    if (componentRegistration.Services
        .OfType<IServiceWithType>()
        .Select(s => new Tuple<Type, TypeInfo>(s.ServiceType, s.ServiceType.GetTypeInfo()))
        .Any(s => !s.Item2.IsInterface || !ProxyUtil.IsAccessible(s.Item1)))
    {
        throw new InvalidOperationException(
            string.Format(
                CultureInfo.CurrentCulture,
                RegistrationExtensionsResources.InterfaceProxyingOnlySupportsInterfaceServices,
                componentRegistration));
    }
}

如果删除 .AsSelf() 调用,代码将起作用。