Skip to content

Whats New In 4.0

Aryeh Citron edited this page Apr 20, 2026 · 5 revisions

UseInMemoryCosmosContainers() Now Uses FakeCosmosHandler

The most significant change in 4.0. UseInMemoryCosmosContainers() now creates a hidden internal CosmosClient backed by FakeCosmosHandler, just like UseInMemoryCosmosDB(). This means:

  • .ToFeedIterator() works natively — no ProductionExtensions package needed
  • Containers resolved from DI are real SDK Container objects (not InMemoryContainer)
  • Full SDK fidelity — serialization, LINQ-to-SQL translation, HTTP request construction, continuation tokens
  • Fault injection available via OnHandlerCreated callback
  • Handler wrapping via WithHttpMessageHandlerWrapper()

The production CosmosClient registration is still not touched — only Container registrations are replaced.

Before (3.x)

// UseInMemoryCosmosContainers registered raw InMemoryContainer instances
services.UseInMemoryCosmosContainers(o => o.AddContainer("orders", "/pk"));
// ❌ Container resolved from DI was InMemoryContainer (no SDK pipeline)
// ❌ .ToFeedIterator() didn't work — needed the legacy ProductionExtensions workaround
// ❌ No fault injection, no request logging

After (4.0)

// Same API — but now backed by FakeCosmosHandler
services.UseInMemoryCosmosContainers(o => o.AddContainer("orders", "/pk"));
// ✅ Container from DI is a real SDK Container
// ✅ .ToFeedIterator() works natively
// ✅ Full SDK fidelity — serialization, LINQ-to-SQL, pagination

New Options

InMemoryContainerOptions now supports:

services.UseInMemoryCosmosContainers(o =>
{
    o.AddContainer("orders", "/pk");
    o.DatabaseName = "TestDb";

    // Capture handler for fault injection or request logging
    o.OnHandlerCreated = (name, handler) =>
    {
        handler.FaultInjector = req => /* ... */;
    };

    // Wrap the HTTP handler (e.g. for logging)
    o.WithHttpMessageHandlerWrapper(h => new MyLoggingHandler(h));
});

Limitations

UseInMemoryCosmosContainers() does not replace the production CosmosClient. This means:

  • If your production code uses CosmosClient to call GetContainer(), those container references will NOT go through the in-memory handler — they'll still reach the production endpoint
  • Only Container instances resolved directly from DI are replaced
  • Use UseInMemoryCosmosDB() instead if your code depends on CosmosClient

InMemoryCosmosClient Deprecated

InMemoryCosmosClient is now marked [Obsolete]. It still works but is no longer recommended.

Why: It bypasses the SDK HTTP pipeline — no serialization testing, no LINQ-to-SQL translation, no fault injection, and .ToFeedIterator() doesn't work without ProductionExtensions.

Migration:

Before After
new InMemoryCosmosClient() InMemoryCosmos.Create(...) or InMemoryCosmos.Builder()...
UseInMemoryCosmosDB<TClient>() with shadow types 4.0.3+: Pass production typed client directly — shadow types removed, full SDK fidelity via Castle.Core proxy. See Pattern 2

ProductionExtensions Package Deprecated

The CosmosDB.InMemoryEmulator.ProductionExtensions NuGet package is no longer needed. All Dependency Injection methods and InMemoryCosmos now use FakeCosmosHandler, which handles .ToFeedIterator() natively.

The package still works for backward compatibility, but new code should not reference it.


RegisterFeedIteratorSetup Deprecated

InMemoryContainerOptions.RegisterFeedIteratorSetup is marked [Obsolete] and ignored. UseInMemoryCosmosContainers() no longer calls InMemoryFeedIteratorSetup.Register() since FakeCosmosHandler handles .ToFeedIterator() natively.


Breaking Changes

Change Impact
UseInMemoryCosmosContainers() containers are real SDK Container (not InMemoryContainer) Code that cast Container to InMemoryContainer will fail. Use OnHandlerCreated to access handler.BackingContainer instead.
UseInMemoryCosmosContainers() no longer calls InMemoryFeedIteratorSetup.Register() Code that relied on the legacy feed iterator extension working after UseInMemoryCosmosContainers() may need to call InMemoryFeedIteratorSetup.Register() explicitly — or better, just use .ToFeedIterator() which now works natively.
InMemoryCosmosClient is [Obsolete] Compilation warnings (or errors with TreatWarningsAsErrors). Suppress with #pragma warning disable CS0618 or <NoWarn>CS0618</NoWarn> while migrating.

See Also

Clone this wiki locally