Skip to main content

RVN009 - Unsupported method call inside index Map/Reduce expression

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 as TextHelpers.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 your Product class defines a custom ComputeKey() method that calculates a value from the product's properties. Calling p.ComputeKey() in the map, where p represents 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:

  • Map and Reduce assignments (including this.Map = … and base.Reduce = …),
    in document indexes that derive from AbstractIndexCreationTask<TDocument>, AbstractIndexCreationTask<TDocument, TReduceResult>, or AbstractMultiMapIndexCreationTask.
    Reduce assignments in multi-map time-series and counters indexes are also checked.

  • AddMap and AddMapForAll calls, 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), and GetHashCode().
    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 AdditionalSources or AdditionalAssemblies:
    assigns the property, sets an entry (AdditionalSources["Key"] = …), or calls Add(…) 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 Equals and ToString methods of a record.
  • Delegate invocations, such as calling a Func<…>.
  • Calls outside the Map, Reduce, AddMap, and AddMapForAll lambdas.
  • 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 in Map or Reduce assignments and AddMap or AddMapForAll calls in a separate method are not checked, even when a constructor calls that method.

    See RVN001 for Map and Reduce assignments outside constructors. RVN001 does not cover AddMap or AddMapForAll calls.

  • 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 Map or Reduce, or passed to AddMap or AddMapForAll, is not checked. For example, RVN009 does not inspect the lambda referenced by Map = 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 using AdditionalAssemblies.

  • Single-map time-series and counters indexes are not checked.

  • RVN009 cannot detect AdditionalSources or AdditionalAssemblies writes 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.

  • RVN010
    The query equivalent: fires when a method declared in your code is called inside a RavenDB query lambda.

In this article