

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# .NET e DAX
<a name="DAX.client.run-application-dotnet"></a>

Completare questa procedura per eseguire l'applicazione di esempio .NET sull'istanza Amazon EC2.

**Nota**  
Questo tutorial utilizza l'SDK.NET 9. Mostra come eseguire un programma nell'Amazon VPC predefinito per accedere al cluster Amazon DynamoDB Accelerator (DAX). Funziona con l'[AWS SDK v4](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/welcome.html) per.NET. Per dettagli sulle modifiche alla V4 e informazioni sulla migrazione, consulta [Migrazione alla versione 4 dell'SDK for AWS .NET](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html). Se preferisci, puoi usare il AWS Toolkit for Visual Studio per scrivere un'applicazione.NET e distribuirla nel tuo VPC.  
Per ulteriori informazioni, consulta [Creazione e implementazione di applicazioni Elastic Beanstalk in .NET mediante AWS Toolkit for Visual Studio](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_NET.html) nella *Guida per gli sviluppatori di AWS Elastic Beanstalk *.

**Come eseguire l'applicazione di esempio .NET per DAX**

1. Vai alla [pagina Microsoft Downloads](https://www.microsoft.com/net/download?initial-os=linux) e scarica l'ultima versione di.NET 9 SDK per Linux. Il file scaricato è `dotnet-sdk-N.N.N-linux-x64.tar.gz`.

1. Estrai i file SDK.

   ```
   mkdir dotnet
   tar zxvf dotnet-sdk-N.N.N-linux-x64.tar.gz -C dotnet
   ```

   Sostituisci `N.N.N` con il numero effettivo della versione dell'SDK .NET (ad esempio: `9.0.305`).

1. Verifica l'installazione.

   ```
   alias dotnet=$HOME/dotnet/dotnet
   dotnet --version
   ```

   Ciò dovrebbe stampare il numero di versione dell'SDK .NET.
**Nota**  
Invece del numero di versione, potresti ricevere l'errore seguente:  
error: libunwind.so.8: cannot open shared object file: No such file or directory  
Per risolvere l'errore, installa il pacchetto `libunwind`.  

   ```
   sudo yum install -y libunwind
   ```
Dopo aver eseguito questa operazione, dovresti essere in grado di eseguire il comando `dotnet --version` senza errori.

1. Crea un nuovo progetto .NET.

   ```
   dotnet new console -o myApp 
   ```

   Questa operazione richiede alcuni minuti per eseguire una one-time-only configurazione. Al termine, esegui il progetto di esempio.

   ```
   dotnet run --project myApp
   ```

   Dovresti ricevere il seguente messaggio: `Hello World!`

1. Il file `myApp/myApp.csproj` contiene metadati sul tuo progetto. Per utilizzare il client DAX nell'applicazione, modificare il file nel modo seguente.

   ```
   <Project Sdk="Microsoft.NET.Sdk">
       <PropertyGroup>
           <OutputType>Exe</OutputType>
           <TargetFramework>net9.0</TargetFramework>
       </PropertyGroup>
       <ItemGroup>
           <PackageReference Include="AWSSDK.DAX.Client" Version="*" />
       </ItemGroup>
   </Project>
   ```

1. Scarica il codice sorgente del programma di esempio (file `.zip`).

   ```
   wget http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/TryDax.zip
   ```

   Al termine del download, estrai i file di origine.

   ```
   unzip TryDax.zip
   ```

1. Ora esegui i programmi di esempio di *dotNet*, uno alla volta. Per ogni programma, copia i suoi contenuti in `myApp/Program.cs` ed esegui il progetto `MyApp`.

   Esegui i seguenti programmi .NET. Il primo programma crea una tabella DynamoDB denominata `TryDaxTable`. Il secondo programma scrive i dati nella tabella.

   ```
   cp TryDax/dotNet/01-CreateTable.cs myApp/Program.cs
   dotnet run --project myApp
   
   cp TryDax/dotNet/02-Write-Data.cs myApp/Program.cs
   dotnet run --project myApp
   ```

1. Quindi, avviare alcuni programmi per eseguire le operazioni `GetItem`, `Query` e `Scan` sul cluster DAX. Per determinare l'endpoint per il cluster DAX, scegli una delle seguenti opzioni:
   +  **Utilizzo della console DynamoDB**: scegli il cluster DAX. L'endpoint del cluster viene visualizzato nella console, come nell'esempio seguente.

     ```
     dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com
     ```
   + **Utilizzando il AWS CLI** — Immettere il seguente comando.

     ```
     aws dax describe-clusters --query "Clusters[*].ClusterDiscoveryEndpoint"
     ```

     L'endpoint del cluster viene visualizzato nell'output, come nell'esempio seguente.

     ```
     {
         "Address": "my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com",
         "Port": 8111,
         "URL": "dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com"
     }
     ```

   Ora esegui i seguenti programmi, specificando il tuo endpoint del cluster come parametro della riga di comando. Sostituisci l'endpoint di esempio con l'endpoint del cluster DAX.

   ```
   cp TryDax/dotNet/03-GetItem-Test.cs myApp/Program.cs
   dotnet run --project myApp dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com
   
   cp TryDax/dotNet/04-Query-Test.cs myApp/Program.cs
   dotnet run --project myApp dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com
                           
   cp TryDax/dotNet/05-Scan-Test.cs myApp/Program.cs
   dotnet run --project myApp dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com
   ```

   Prendi nota delle informazioni sui tempi: il numero di millisecondi richiesto per i test `GetItem`, `Query` e `Scan`.

1. Esegui il seguente programma .NET per eliminare `TryDaxTable`:

   ```
   cp TryDax/dotNet/06-DeleteTable.cs myApp/Program.cs
   dotnet run --project myApp
   ```

Per ulteriori informazioni sui programmi, consulta le seguenti sezioni:
+ [0-1 csCreateTable.](DAX.client.run-application-dotnet.01-CreateTable.md)
+ [02-Write-Data.cs](DAX.client.run-application-dotnet.02-Write-Data.md)
+ [03- -Test.cs GetItem](DAX.client.run-application-dotnet.03-GetItem-Test.md)
+ [04-Query-Test.cs](DAX.client.run-application-dotnet.04-Query-Test.md)
+ [05-Scan-Test.cs](DAX.client.run-application-dotnet.05-Scan-Test.md)
+ [DeleteTable0-6 pezzi](DAX.client.run-application-dotnet.06-DeleteTable.md)

# 0-1 csCreateTable.
<a name="DAX.client.run-application-dotnet.01-CreateTable"></a>

Il programma `01-CreateTable.cs` crea una tabella (`TryDaxTable`). I programmi .NET rimanenti in questa sezione dipendono da questa tabella.

```
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;

namespace ClientTest
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            AmazonDynamoDBClient client = new AmazonDynamoDBClient();

            var tableName = "TryDaxTable";

            var request = new CreateTableRequest()
            {
                TableName = tableName,
                KeySchema = new List<KeySchemaElement>()
                {
                    new KeySchemaElement{ AttributeName = "pk",KeyType = "HASH"},
                    new KeySchemaElement{ AttributeName = "sk",KeyType = "RANGE"}
                },
                AttributeDefinitions = new List<AttributeDefinition>() {
                    new AttributeDefinition{ AttributeName = "pk",AttributeType = "N"},
                    new AttributeDefinition{ AttributeName = "sk",AttributeType  = "N"}
                },
                ProvisionedThroughput = new ProvisionedThroughput()
                {
                    ReadCapacityUnits = 10,
                    WriteCapacityUnits = 10
                }
            };

            var response = await client.CreateTableAsync(request);

            Console.WriteLine("Hit <enter> to continue...");
            Console.ReadLine();
        }
    }
}
```

# 02-Write-Data.cs
<a name="DAX.client.run-application-dotnet.02-Write-Data"></a>

Il programma `02-Write-Data.cs` scrive i dati di test in `TryDaxTable`.

```
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;

namespace ClientTest
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            AmazonDynamoDBClient client = new AmazonDynamoDBClient();

            var tableName = "TryDaxTable";

            string someData = new string('X', 1000);
            var pkmax = 10;
            var skmax = 10;

            for (var ipk = 1; ipk <= pkmax; ipk++)
            {
                Console.WriteLine($"Writing {skmax} items for partition key: {ipk}");
                for (var isk = 1; isk <= skmax; isk++)
                {
                    var request = new PutItemRequest()
                    {
                        TableName = tableName,
                        Item = new Dictionary<string, AttributeValue>()
                       {
                            { "pk", new AttributeValue{N = ipk.ToString() } },
                            { "sk", new AttributeValue{N = isk.ToString() } },
                            { "someData", new AttributeValue{S = someData } }
                       }
                    };

                    var response = await client.PutItemAsync(request);
                }
            }

            Console.WriteLine("Hit <enter> to continue...");
            Console.ReadLine();
        }
    }
}
```

# 03- -Test.cs GetItem
<a name="DAX.client.run-application-dotnet.03-GetItem-Test"></a>

Il programma `03-GetItem-Test.cs` esegue le operazioni `GetItem` in `TryDaxTable`.

```
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.DAX;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;

namespace ClientTest
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            string endpointUri = args[0];
            Console.WriteLine($"Using DAX client - endpointUri={endpointUri}");

            var clientConfig = new DaxClientConfig(endpointUri)
            {
                AwsCredentials = FallbackCredentialsFactory.GetCredentials()
            };
            var client = new ClusterDaxClient(clientConfig);

            var tableName = "TryDaxTable";

            var pk = 1;
            var sk = 10;
            var iterations = 5;

            var startTime = System.DateTime.Now;

            for (var i = 0; i < iterations; i++)
            {
                for (var ipk = 1; ipk <= pk; ipk++)
                {
                    for (var isk = 1; isk <= sk; isk++)
                    {
                        var request = new GetItemRequest()
                        {
                            TableName = tableName,
                            Key = new Dictionary<string, AttributeValue>() {
                            {"pk", new AttributeValue {N = ipk.ToString()} },
                            {"sk", new AttributeValue {N = isk.ToString() } }
                        }
                        };
                        var response = await client.GetItemAsync(request);
                        Console.WriteLine($"GetItem succeeded for pk: {ipk},sk: {isk}");
                    }
                }
            }

            var endTime = DateTime.Now;
            TimeSpan timeSpan = endTime - startTime;
            Console.WriteLine($"Total time: {timeSpan.TotalMilliseconds} milliseconds");

            Console.WriteLine("Hit <enter> to continue...");
            Console.ReadLine();
        }
    }
}
```

# 04-Query-Test.cs
<a name="DAX.client.run-application-dotnet.04-Query-Test"></a>

Il programma `04-Query-Test.cs` esegue le operazioni `Query` in `TryDaxTable`.

```
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.DAX;
using Amazon.DynamoDBv2.Model;

namespace ClientTest
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            string endpointUri = args[0];
            Console.WriteLine($"Using DAX client - endpointUri={endpointUri}");


            var clientConfig = new DaxClientConfig(endpointUri)
            {
                AwsCredentials = FallbackCredentialsFactory.GetCredentials()
            };
            var client = new ClusterDaxClient(clientConfig);

            var tableName = "TryDaxTable";

            var pk = 5;
            var sk1 = 2;
            var sk2 = 9;
            var iterations = 5;

            var startTime = DateTime.Now;

            for (var i = 0; i < iterations; i++)
            {
                var request = new QueryRequest()
                {
                    TableName = tableName,
                    KeyConditionExpression = "pk = :pkval and sk between :skval1 and :skval2",
                    ExpressionAttributeValues = new Dictionary<string, AttributeValue>() {
                            {":pkval", new AttributeValue {N = pk.ToString()} },
                            {":skval1", new AttributeValue {N = sk1.ToString()} },
                            {":skval2", new AttributeValue {N = sk2.ToString()} }
                    }
                };
                var response = await client.QueryAsync(request);
                Console.WriteLine($"{i}: Query succeeded");

            }

            var endTime = DateTime.Now;
            TimeSpan timeSpan = endTime - startTime;
            Console.WriteLine($"Total time: {timeSpan.TotalMilliseconds} milliseconds");

            Console.WriteLine("Hit <enter> to continue...");
            Console.ReadLine();
        }
    }
}
```

# 05-Scan-Test.cs
<a name="DAX.client.run-application-dotnet.05-Scan-Test"></a>

Il programma `05-Scan-Test.cs` esegue le operazioni `Scan` in `TryDaxTable`.

```
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.DAX;
using Amazon.DynamoDBv2.Model;

namespace ClientTest
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            string endpointUri = args[0];
            Console.WriteLine($"Using DAX client - endpointUri={endpointUri}");

            var clientConfig = new DaxClientConfig(endpointUri)
            {
                AwsCredentials = FallbackCredentialsFactory.GetCredentials()
            };
            var client = new ClusterDaxClient(clientConfig);

            var tableName = "TryDaxTable";

            var iterations = 5;

            var startTime = DateTime.Now;

            for (var i = 0; i < iterations; i++)
            {
                var request = new ScanRequest()
                {
                    TableName = tableName
                };
                var response = await client.ScanAsync(request);
                Console.WriteLine($"{i}: Scan succeeded");
            }

            var endTime = DateTime.Now;
            TimeSpan timeSpan = endTime - startTime;
            Console.WriteLine($"Total time: {timeSpan.TotalMilliseconds} milliseconds");

            Console.WriteLine("Hit <enter> to continue...");
            Console.ReadLine();
        }
    }
}
```

# DeleteTable0-6 pezzi
<a name="DAX.client.run-application-dotnet.06-DeleteTable"></a>

Il programma `06-DeleteTable.cs` elimina `TryDaxTable`. Esegui questo programma dopo aver completato i test.

```
using System;
using System.Threading.Tasks;
using Amazon.DynamoDBv2.Model;
using Amazon.DynamoDBv2;

namespace ClientTest
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            AmazonDynamoDBClient client = new AmazonDynamoDBClient();

            var tableName = "TryDaxTable";

            var request = new DeleteTableRequest()
            {
                TableName = tableName
            };

            var response = await client.DeleteTableAsync(request);

            Console.WriteLine("Hit <enter> to continue...");
            Console.ReadLine();
        }
    }
}
```