title: Connection to cosmosdb fails in runtime when wrapped in a function
body: <p>I get this runtime error when I wrap connecting to CosmosDB in af function.</p>
  <p>&quot;Microsoft.Azure.Cosmos.CosmosObjectDisposedException: 'Cannot access a disposed 'CosmosClient'. Follow best practices and use the CosmosClient as a singleton.&quot;</p>
  <p>It works fine if its not inside a function.</p>
  <p>Why is that?</p>
  <p>This is my function:</p>
  <pre><code>    public static async Task&lt;Container&gt; connectToCosmosAsync()
      {
          // New instance of CosmosClient class
          using CosmosClient client = new(
              accountEndpoint: &quot;https://xxxxxxxxxxxxxxx/&quot;,
              authKeyOrResourceToken: &quot;=xxxxxxxxxxxxx=&quot;
          );
  
          // Database reference with creation if it does not already exist
          Database database = await client.CreateDatabaseIfNotExistsAsync(
              id: &quot;uniconta&quot;
          );
  
          Console.WriteLine($&quot;New database:\t{database.Id}&quot;);
  
          // Container reference with creation if it does not alredy exist
          Container container = await database.CreateContainerIfNotExistsAsync(
              id: &quot;uniconta&quot;,
              partitionKeyPath: &quot;/id&quot;,
              throughput: 400
          );
  
          Console.WriteLine($&quot;New container:\t{container.Id}&quot;);
  
          return container;
      }
  </code></pre>