@@ -19,28 +19,35 @@ internal static class ContainerPropertiesExtensions
1919 {
2020 private const int DefaultStreamBufferSize = 4096 ;
2121
22- internal static Task < PartitionKey ? > EnsureIdGetAppendedToPartitionKeyIfNeededAsync (
22+ internal static async Task < PartitionKey ? > EnsureIdGetAppendedToPartitionKeyIfNeededAsync (
2323 this ContainerInternal container ,
2424 PartitionKey ? partitionKey ,
2525 string itemId ,
2626 CancellationToken cancellationToken )
2727 {
2828 if ( container == null )
2929 {
30- return Task . FromResult ( partitionKey ) ;
30+ return partitionKey ;
3131 }
3232
33- // Only use container properties that are already cached in memory.
34- // This avoids triggering client initialization (EnsureValidClientAsync)
35- // or making HTTP calls (ResolveByNameAsync) before the handler pipeline
36- // has had a chance to initialize the client.
37- // On subsequent requests, the cache will be populated by the normal
38- // request pipeline and this optimization will take effect.
39- if ( ! container . TryGetCachedContainerPropertiesFromMemory ( out ContainerProperties containerProperties )
40- || containerProperties == null
41- || ! containerProperties . IsLastPartitionKeyPathId )
33+ ContainerProperties containerProperties ;
34+ try
35+ {
36+ containerProperties = await container . GetCachedContainerPropertiesAsync (
37+ forceRefresh : false ,
38+ trace : NoOpTrace . Singleton ,
39+ cancellationToken : cancellationToken ) ;
40+ }
41+ catch ( Exception )
42+ {
43+ // Container may not exist yet (e.g. operations on non-existent containers).
44+ // Swallow the exception and let the actual operation surface the proper error.
45+ return partitionKey ;
46+ }
47+
48+ if ( containerProperties == null || ! containerProperties . IsLastPartitionKeyPathId )
4249 {
43- return Task . FromResult ( partitionKey ) ;
50+ return partitionKey ;
4451 }
4552
4653 if ( string . IsNullOrEmpty ( itemId ) )
@@ -56,7 +63,7 @@ internal static class ContainerPropertiesExtensions
5663
5764 if ( existingComponents . Count != partitionKeyPaths . Count - 1 )
5865 {
59- return Task . FromResult ( partitionKey ) ;
66+ return partitionKey ;
6067 }
6168 }
6269
@@ -86,7 +93,7 @@ internal static class ContainerPropertiesExtensions
8693
8794 Documents . Routing . PartitionKeyInternal partitionKeyInternal = new Documents . Routing . PartitionKeyInternal ( allComponentsList ) ;
8895
89- return Task . FromResult < PartitionKey ? > ( new PartitionKey ( partitionKeyInternal ) ) ;
96+ return new PartitionKey ( partitionKeyInternal ) ;
9097 }
9198
9299 internal static async Task < ( string , Stream ) > GetItemIdFromStreamIfRequiredAsync (
0 commit comments