C# Autofac.Extras.DynamicProxy在公共接口中出错

C# Autofac.Extras.DynamicProxy在公共接口中出错,c#,dependency-injection,autofac,castle-dynamicproxy,C#,Dependency Injection,Autofac,Castle Dynamicproxy,我有公共接口和内部impl public interface IService { ... } internal class Service: IService { ... } 我通过 builder.RegisterAssemblyTypes(assembly) .EnableInterfaceInterceptors() .AsImplementedInterfaces() .AsSelf() 但我犯了个错误 组件激活器=服务(R

我有公共接口和内部impl

public interface IService
{
    ...
}
internal class Service: IService
{
    ...
}
我通过

builder.RegisterAssemblyTypes(assembly)
       .EnableInterfaceInterceptors()
       .AsImplementedInterfaces()
       .AsSelf()
但我犯了个错误

组件激活器=服务(ReflectionActivator),服务=[~.IService,~.Service], Lifetime=Autofac.Core.Lifetime.MatchingScopeLifetime,Sharing=Shared,Ownership=OwnedByLifetimeScope无法使用接口拦截,因为它提供的服务不是公开可见的接口。检查组件的注册情况,确保未启用拦截并将其注册为内部/专用接口类型


为什么我会犯这个错误?我的界面是公共的。

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

错误消息是

无法使用接口拦截,因为它提供的服务不是公开可见的接口

这意味着注册的所有服务都应该是一个接口,但事实并非如此。你可以在地图上看到它

private static void确保接口拦截应用程序(IComponentRegistration组件注册)
{
if(componentRegistration.Services
第()类
.Select(s=>newtuple(s.ServiceType,s.ServiceType.GetTypeInfo())
.Any(s=>!s.Item2.i接口| |!ProxyUtil.IsAccessible(s.Item1)))
{
抛出新的InvalidOperationException(
字符串格式(
CultureInfo.CurrentCulture,
RegistrationExtensionsResources.InterfaceProxingOnlySupportsinterFaceServices,
组件注册);
}
}

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

谢谢,但即使接口和impl都是公共的,我也会不断遇到这个错误。我对此感到困惑。@Horosho我也可以用公共接口重现它。如果您删除
AsSelf()
调用,它是否工作?错误消息是不公开可见的接口,可以翻译为public和interfaceeyes,它在没有AsSelf()的情况下工作,现在我发现了我的错误。
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));
    }
}