RVN009 - Unsupported method call inside index Map/Reduce expression
-
RVN009 reports calls to methods declared in your own code inside an index's
Map,Reduce,AddMap,
orAddMapForAlllambda. -
Default severity: Info
Automated code fix: No -
In this article:
Description
An index's Map and Reduce expressions run on the server, not in your application.
The client sends them to the server as C# source text, and the server compiles that text when the index is deployed.
Calls to custom methods:
Methods declared in your own code are not part of that source, so the server cannot call them.
Depending on the call, one of the following happens:
-
The server fails to compile the index, and deploying it throws
IndexCompilationException.
For example, a call to a static helper such asTextHelpers.Normalize(p.Name). -
The index compiles, but your method never runs:
on the server, documents are dynamic JSON objects, not instances of your classes.
For example, suppose yourProductclass defines a customComputeKey()method that calculates a value from the product's properties. Callingp.ComputeKey()in the map, whereprepresents a product document, does not execute that method. The index field does not get the value your method would compute.
Supported methods:
The server can compile LINQ operators, standard .NET methods such as string and Math methods,
and RavenDB indexing methods such as LoadDocument and CreateField.
To run your own logic on the server, you can deploy a static helper with the index using AdditionalSources or AdditionalAssemblies.
Trigger condition
When is RVN009 reported?
RVN009 checks the lambdas that define the index in the constructors of the index class:
-
MapandReduceassignments (includingthis.Map = …andbase.Reduce = …),
in document indexes that derive fromAbstractIndexCreationTask<TDocument>,AbstractIndexCreationTask<TDocument, TReduceResult>, orAbstractMultiMapIndexCreationTask.
Reduceassignments in multi-map time-series and counters indexes are also checked. -
AddMapandAddMapForAllcalls, in multi-map indexes.
In these lambdas, including nested lambdas, RVN009 reports each call to a method declared in the source code of the project that contains the index:
- Static methods, such as a helper in a utility class or in the index class itself.
- Instance methods of your own types, such as a method of the document class.
- Extension methods you declared.
- User-written overrides of
ToString(),Equals(object), andGetHashCode().
When called on a document, these run on the server's JSON object, so your override is not used.
For example,p.ToString()returns the document's JSON text.
Reported on the method name.
When is RVN009 not reported?
- The index class, or one of its base classes, writes to
AdditionalSourcesorAdditionalAssemblies:
assigns the property, sets an entry (AdditionalSources["Key"] = …), or callsAdd(…)on it.
RVN009 then assumes the server can compile the methods the index calls, and reports nothing for that index.
Only reading these properties does not count.
RVN009 does not verify that the additional code actually provides the called methods. - Members generated by the compiler, such as the
EqualsandToStringmethods of a record. - Delegate invocations, such as calling a
Func<…>. - Calls outside the
Map,Reduce,AddMap, andAddMapForAlllambdas. - JavaScript indexes (
AbstractJavaScriptIndexCreationTask), which define their maps as strings.
How to resolve
Do one of the following:
- Inline the method's logic in the lambda, using only constructs the server can compile.
- Use a static helper that takes the document values it needs as parameters, for example
KeyHelpers.ComputeKey(p.Name), and deploy the helper with the index using AdditionalSources or AdditionalAssemblies.
Deploying the document class itself does not make calls to its instance methods, such as p.ComputeKey(), work.
On the server, p is a dynamic JSON object, not an instance of your class.
When RVN009 detects a write to AdditionalSources or AdditionalAssemblies, it suppresses diagnostics for the whole index.
This can hide calls such as p.ComputeKey() that still do not run your method.
Before (triggers RVN009)
public static class TextHelpers
{
public static string Normalize(string s) => s.Trim().ToLower();
}
public class Products_ByName : AbstractIndexCreationTask<Product>
{
public Products_ByName()
{
Map = products => from p in products
// Triggers RVN009: Normalize is declared in your code.
select new { Name = TextHelpers.Normalize(p.Name) };
}
}
After (correct)
public class Products_ByName : AbstractIndexCreationTask<Product>
{
public Products_ByName()
{
Map = products => from p in products
select new { Name = p.Name.Trim().ToLower() };
}
}
Limitations
-
RVN009 checks only lambdas in constructor bodies.
Lambdas inMaporReduceassignments andAddMaporAddMapForAllcalls in a separate method are not checked, even when a constructor calls that method.See RVN001 for
MapandReduceassignments outside constructors. RVN001 does not coverAddMaporAddMapForAllcalls. -
A partial class declaration that lists neither a base class nor any interfaces is skipped. Lambdas in constructors declared in that part are not checked, even when another part declares the index base class.
-
A lambda stored in a variable and then assigned to
MaporReduce, or passed toAddMaporAddMapForAll, is not checked. For example, RVN009 does not inspect the lambda referenced byMap = map;. -
Methods from compiled assemblies, such as a NuGet package or a referenced DLL, are not reported,
although the server cannot call them either unless the index deploys them usingAdditionalAssemblies. -
Single-map time-series and counters indexes are not checked.
-
RVN009 cannot detect
AdditionalSourcesorAdditionalAssemblieswrites in a base class from another assembly when neither its source nor usable generated index metadata can be read.
In that case, it can report a call even when the base class deploys the called method.
Related C# analyzers
- RVN010
The query equivalent: fires when a method declared in your code is called inside a RavenDB query lambda.