You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Listeners: What are conflict listeners and how to work with them?

To allow users to handle document replication conflicts automatically, we introduced a Document Conflict listener. To create your own listener of this type, just implement IDocumentConflictListener interface.

public interface IDocumentConflictListener
{
	bool TryResolveConflict(string key, JsonDocument[] conflictedDocs, 
							out JsonDocument resolvedDocument);
}

Example

This example shows how to create an automatic conflict resolver by using IDocumentConflictListener, which will pick the newest item from the list of conflicted documents:

public class ResolveInFavourOfNewest : IDocumentConflictListener
{
	public bool TryResolveConflict(
		string key,
		JsonDocument[] conflictedDocs,
		out JsonDocument resolvedDocument)
	{
		DateTime? maxDate = conflictedDocs.Max(x => x.LastModified);
		resolvedDocument = conflictedDocs
							.FirstOrDefault(x => x.LastModified == maxDate);

		if (resolvedDocument == null)
			return false;

		resolvedDocument.Metadata.Remove("@id");
		resolvedDocument.Metadata.Remove("@etag");
		return true;
	}
}