gpt4 book ai didi

c# - Autofac.Extras.DynamicProxy 公共(public)接口(interface)出错

转载 作者:行者123 更新时间:2023-11-30 23:05:19 27 4
gpt4 key购买 nike

我有公共(public)接口(interface)和内部实现,

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.

为什么会出现此错误?我的界面是公开的。

最佳答案

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

错误信息是

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

意思是注册的所有服务都应该是一个接口(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() 调用,代码将正常工作。

关于c# - Autofac.Extras.DynamicProxy 公共(public)接口(interface)出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com