Episerver11:Content Delivery API的CustomContentResultService未调用
解决Episerver 11中CustomContentResultService未被调用的问题
1. 修正依赖注入的注册方式
你当前用AddTransient添加自定义实现,只会新增一个服务实例,默认的ContentResultService仍会被容器优先选用。需要改为替换默认服务的方式:
public void ConfigureContainer(ServiceConfigurationContext context) { // 替换默认的ContentResultService为自定义实现 context.Services.Replace(ServiceDescriptor.Transient<ContentResultService, CustomContentResultService>()); context.Services.AddTransient<IContentRenderer, ErrorHandlingContentRenderer>() .AddTransient<ContentAreaRenderer, XYZContentAreaRenderer>(); context.Services.Configure<ContentApiConfiguration>(config => { config.Default() .SetMinimumRoles(string.Empty) .SetMultiSiteFilteringEnabled(false) .SetSiteDefinitionApiEnabled(true) .SetIncludeMasterLanguage(false) .SetFlattenPropertyModel(true) .SetIncludeNullValues(false) .SetValidateTemplateForContentUrl(false); }); context.ConfigurationComplete += (o, e) => { // 保留其他需在配置完成后注册的服务逻辑 }; }
2. 验证CustomContentResultService的继承与构造函数
确保自定义类正确继承ContentResultService,且构造函数正确传递基类所需依赖:
public class CustomContentResultService : ContentResultService { public CustomContentResultService(IContentLoader contentLoader, IContentModelMapper contentModelMapper, IUrlResolver urlResolver, ContentApiConfiguration contentApiConfiguration) : base(contentLoader, contentModelMapper, urlResolver, contentApiConfiguration) { } // 重写需要自定义的方法,比如GetContentResult public override IContentResult GetContentResult(IContent content, Language language, bool expand = false) { var result = base.GetContentResult(content, language, expand); // 移除前端不需要的冗余字段 result.Properties.Remove("冗余字段名"); return result; } }
3. 检查Content Delivery API版本兼容性
Episerver 11对应的Content Delivery API应为2.x系列,确认NuGet包版本与Episerver 11兼容,避免版本不匹配导致服务注册失效。
4. 调整依赖解析器配置
Episerver 11内置的依赖注入容器已足够处理服务解析,手动设置DependencyResolver可能干扰服务定位,尝试注释掉以下代码:
// 注释该行代码 // DependencyResolver.SetResolver(new ServiceLocatorDependencyResolver(context.Locate.Advanced));
5. 清除缓存并重启应用
修改依赖注入配置后,清除Episerver的对象缓存、视图缓存,再重启应用程序,确保新的服务注册生效。
内容的提问来源于stack exchange,提问作者Farhin Shaikh
相关产品推荐
相关产品推荐

