AWS Builder Center

Building Your First Generative AI App with .NET 8 and Amazon Bedrock

Learn how to integrate Amazon Bedrock into your .NET applications using the AWS SDK. This guide covers setting up the AWSSDK.BedrockRuntime package and using the Converse API to generate text with C#.

Backend Engineer · C# / .NET · REST APIs · MS-SQL · Fast API · LLM · RAG · M.Sc. IT @ TH OWL
Are you a .NET developer looking to dive into the world of Artificial Intelligence? Amazon Bedrock makes it incredibly straightforward to integrate Generative AI into your C# applications. Think of Bedrock as a universal adapter for foundation models. It gives you a single, consistent interface to access models from providers like Anthropic, Meta, and Amazon, without needing to juggle separate APIs for each one.
In this quick guide, we will build a simple console application that uses the AWS SDK for .NET to send a prompt to an AI model and print the response.

Prerequisites

  • An active AWS Account with model access enabled in Amazon Bedrock.
  • AWS credentials configured locally (e.g., via the AWS CLI using  aws configure ).
  • .NET 8 SDK installed on your machine.

Step 1: Set Up Your Project

First, open your terminal and create a new console application:  dotnet new console -n BedrockDotNetAI   cd BedrockDotNetAI 
Next, add the required AWS SDK package. We need the runtime package to actually invoke the AI models:  dotnet add package AWSSDK.BedrockRuntime 

Step 2: Write the Integration Code

Open your  Program.cs  file and replace the contents with the following code. We will use the  ConverseAsync  API, which provides a clean and modern way to send text messages to Bedrock models.
using Amazon;
using Amazon.BedrockRuntime;
using Amazon.BedrockRuntime.Model;

// 1. Initialize the Bedrock Runtime Client
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USWest2);

// 2. Define the Model ID (Ensure you have requested access to this model in the AWS console)
var modelId = "amazon.nova-lite-v1:0";

// 3. Create the Request using the Converse API
var request = new ConverseRequest
{
ModelId = modelId,
Messages = new List<Message>
{
new Message
{
Role = ConversationRole.User,
Content = new List<ContentBlock>
{
new ContentBlock { Text = "Explain how async/await works in .NET in two short sentences." }
}
}
}
};

try
{
// 4. Send the request and wait for the response
Console.WriteLine("Sending request to Amazon Bedrock...");
var response = await client.ConverseAsync(request);

// 5. Extract and display the text
string responseText = response?.Output?.Message?.Content?[0]?.Text ?? "No response generated.";
Console.WriteLine("\nResponse:");
Console.WriteLine(responseText);
}
catch (Exception ex)
{
Console.WriteLine($"\nError: {ex.Message}");
}
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article