IRuleDeviceInformationManager error when applying personalization rules on CM servers


Description

Adding, editing the personalization rule, or visiting a page with setup personalization might fail with an unhandled exception. This happens only when using Device Detection Rules on CM server. An error similar to the following can be found in log records:

ERROR Error in Mobile device processing.
Exception: System.Reflection.TargetInvocationException
Message: Exception has been thrown by the target of an invocation.
Source: mscorlib
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Sitecore.Reflection.ReflectionUtil.CreateObject(Type type)
at Sitecore.Rules.DefaultRuleFactory.GetRuleCondition[T](Database database, XElement element)
at Sitecore.Rules.DefaultRuleFactory.GetRuleConditions[T](Database database, XElement element, Rule`1 result)
at Sitecore.Rules.DefaultRuleFactory.GetRule[T](Database database, XElement element)
at Sitecore.Rules.DefaultRuleFactory.ParseRules[T](Database database, XElement node)
at Sitecore.Rules.DefaultRuleFactory.GetRules[T](Field field)
at Sitecore.Rules.DefaultRuleFactory.GetRules[T](IEnumerable`1 items, String fieldName)
at Sitecore.Data.Items.DeviceItem.MatchesRules(HttpContextBase httpContext, SafeDictionary`2 customData)
at Sitecore.Data.Items.DeviceItem.ResolveHelper.FindBestMatch(Database database, HttpContextBase httpContext)
Nested Exception
Exception: System.InvalidOperationException
Message: No service for type 'Sitecore.CES.DeviceDetection.Rules.IRuleDeviceInformationManager' has been registered.
Source: Microsoft.Extensions.DependencyInjection.Abstractions
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Sitecore.ContentTesting.Rules.Conditions.DeviceTypeCondition`1..ctor()

The issue occurs due to the configuration files in \App_Config\Sitecore\DeviceDetection.Client\. The configuration from the files in this folder is applied to Standalone, Content Delivery, and Processing roles only.

Solution

To resolve the issue apply the following changes to all the files from the App_Config\Sitecore\DeviceDetection.Client folder:

Replace

<sitecore role:require="Standalone or ContentDelivery or Processing">

with

<sitecore role:require="Standalone or ContentDelivery or Processing or ContentManagement">