Get started with Relay Hybrid Connections HTTP requests in .NET
In this quickstart, you create .NET sender and receiver applications that send and receive messages by using the HTTP protocol. The applications use Hybrid Connections feature of Azure Relay. To learn about Azure Relay in general, see Azure Relay.
In this quickstart, you take the following steps:
- Create a Relay namespace by using the Azure portal.
- Create a hybrid connection in that namespace by using the Azure portal.
- Write a server (listener) console application to receive messages.
- Write a client (sender) console application to send messages.
- Run applications.
Prerequisites
To complete this tutorial, you need the following prerequisites:
- Visual Studio 2019 or later. The examples in this tutorial use Visual Studio 2022.
- An Azure subscription. If you don't have one, create a trial subscription before you begin.
Create a namespace
Sign in to the Azure portal.
Select All services on the left menu. Select Integration, search for Relays, move the mouse over Relays, and then select Create.
On the Create namespace page, follow these steps:
Choose an Azure subscription in which to create the namespace.
For Resource group, choose an existing resource group in which to place the namespace, or create a new one.
Enter a name for the Relay namespace.
Select the region in which your namespace should be hosted.
Select Review + create at the bottom of the page.
On the Review + create page, select Create.
After a few minutes, you see the Relay page for the namespace.
Get management credentials
On the Relay page, select Shared access policies on the left menu.
On the Shared access policies page, select RootManageSharedAccessKey.
Under SAS Policy: RootManageSharedAccessKey, select the Copy button next to Primary Connection String. This action copies the connection string to your clipboard for later use. Paste this value into Notepad or some other temporary location.
Repeat the preceding step to copy and paste the value of Primary key to a temporary location for later use.
Create a hybrid connection
On the Relay page for your namespace, follow these steps to create a hybrid connection.
On the left menu, Under Entities, select Hybrid Connections, and then select + Hybrid Connection.
On the Create Hybrid Connection page, enter a name for the hybrid connection, and select Create.
Create a server application (listener)
In Visual Studio, write a C# console application to listen for and receive messages from the relay.
Create a console application
In Visual Studio, create a new Console App (.NET Framework) project.
Add the Relay NuGet package
Right-click the newly created project, and then select Manage NuGet Packages.
Select Browse, and then search for Microsoft.Azure.Relay. In the search results, select Microsoft Azure Relay.
Select Install to complete the installation. Close the dialog box.
Write code to receive messages
At the top of the Program.cs file, replace the existing
using
statements with the followingusing
statements:using System; using System.IO; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Relay; using System.Net;
Add constants to the
Program
class for the hybrid connection details. Replace the placeholders in brackets with the values that you obtained when you created the hybrid connection. Be sure to use the fully qualified namespace name.// replace {RelayNamespace} with the name of your namespace private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn"; // replace {HybridConnectionName} with the name of your hybrid connection private const string ConnectionName = "{HybridConnectionName}"; // replace {SAKKeyName} with the name of your Shared Access Policies key, which is RootManageSharedAccessKey by default private const string KeyName = "{SASKeyName}"; // replace {SASKey} with the primary key of the namespace you saved earlier private const string Key = "{SASKey}";
Add the
RunAsync
method to theProgram
class:private static async Task RunAsync() { var cts = new CancellationTokenSource(); var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key); var listener = new HybridConnectionListener(new Uri(string.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider); // Subscribe to the status events. listener.Connecting += (o, e) => { Console.WriteLine("Connecting"); }; listener.Offline += (o, e) => { Console.WriteLine("Offline"); }; listener.Online += (o, e) => { Console.WriteLine("Online"); }; // Provide an HTTP request handler listener.RequestHandler = (context) => { // Do something with context.Request.Url, HttpMethod, Headers, InputStream... context.Response.StatusCode = HttpStatusCode.OK; context.Response.StatusDescription = "OK, This is pretty neat"; using (var sw = new StreamWriter(context.Response.OutputStream)) { sw.WriteLine("hello!"); } // The context MUST be closed here context.Response.Close(); }; // Opening the listener establishes the control channel to // the Azure Relay service. The control channel is continuously // maintained, and is reestablished when connectivity is disrupted. await listener.OpenAsync(); Console.WriteLine("Server listening"); // Start a new thread that will continuously read the console. await Console.In.ReadLineAsync(); // Close the listener after you exit the processing loop. await listener.CloseAsync(); }
Add the following line of code to the
Main
method in theProgram
class:RunAsync().GetAwaiter().GetResult();
The completed Program.cs file should look like this:
namespace Server { using System; using System.IO; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Relay; using System.Net; public class Program { private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn"; private const string ConnectionName = "{HybridConnectionName}"; private const string KeyName = "{SASKeyName}"; private const string Key = "{SASKey}"; public static void Main(string[] args) { RunAsync().GetAwaiter().GetResult(); } private static async Task RunAsync() { var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key); var listener = new HybridConnectionListener(new Uri(string.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider); // Subscribe to the status events. listener.Connecting += (o, e) => { Console.WriteLine("Connecting"); }; listener.Offline += (o, e) => { Console.WriteLine("Offline"); }; listener.Online += (o, e) => { Console.WriteLine("Online"); }; // Provide an HTTP request handler listener.RequestHandler = (context) => { // Do something with context.Request.Url, HttpMethod, Headers, InputStream... context.Response.StatusCode = HttpStatusCode.OK; context.Response.StatusDescription = "OK"; using (var sw = new StreamWriter(context.Response.OutputStream)) { sw.WriteLine("hello!"); } // The context MUST be closed here context.Response.Close(); }; // Opening the listener establishes the control channel to // the Azure Relay service. The control channel is continuously // maintained, and is reestablished when connectivity is disrupted. await listener.OpenAsync(); Console.WriteLine("Server listening"); // Start a new thread that will continuously read the console. await Console.In.ReadLineAsync(); // Close the listener after you exit the processing loop. await listener.CloseAsync(); } } }
Create a client application (sender)
In Visual Studio, write a C# console application to send messages to the relay.
Create a console application
If you disabled the "Requires Client Authorization" option when creating the Relay,
you can send requests to the Hybrid Connections URL with any browser. For accessing
protected endpoints, you need to create and pass a token in the ServiceBusAuthorization
header, which is shown here.
In Visual Studio, create a new Console App (.NET Framework) project.
Add the Relay NuGet package
Right-click the newly created project, and then select Manage NuGet Packages.
Select Include prerelease option.
Select Browse, and then search for Microsoft.Azure.Relay. In the search results, select Microsoft Azure Relay.
Select Install to complete the installation. Close the dialog box.
Write code to send requests
At the top of the Program.cs file, replace the existing
using
statements with the followingusing
statements:using System; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Net.Http; using Microsoft.Azure.Relay;
Add constants to the
Program
class for the hybrid connection details. Replace the placeholders in brackets with the values that you obtained when you created the hybrid connection. Be sure to use the fully qualified namespace name.// replace {RelayNamespace} with the name of your namespace private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn"; // replace {HybridConnectionName} with the name of your hybrid connection private const string ConnectionName = "{HybridConnectionName}"; // replace {SAKKeyName} with the name of your Shared Access Policies key, which is RootManageSharedAccessKey by default private const string KeyName = "{SASKeyName}"; // replace {SASKey} with the primary key of the namespace you saved earlier private const string Key = "{SASKey}";
Add the following method to the
Program
class:private static async Task RunAsync() { var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( KeyName, Key); var uri = new Uri(string.Format("https://{0}/{1}", RelayNamespace, ConnectionName)); var token = (await tokenProvider.GetTokenAsync(uri.AbsoluteUri, TimeSpan.FromHours(1))).TokenString; var client = new HttpClient(); var request = new HttpRequestMessage() { RequestUri = uri, Method = HttpMethod.Get, }; request.Headers.Add("ServiceBusAuthorization", token); var response = await client.SendAsync(request); Console.WriteLine(await response.Content.ReadAsStringAsync()); Console.ReadLine(); }
Add the following line of code to the
Main
method in theProgram
class.RunAsync().GetAwaiter().GetResult();
The Program.cs should look like this:
using System; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Net.Http; using Microsoft.Azure.Relay; namespace Client { class Program { private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn"; private const string ConnectionName = "{HybridConnectionName}"; private const string KeyName = "{SASKeyName}"; private const string Key = "{SASKey}"; static void Main(string[] args) { RunAsync().GetAwaiter().GetResult(); } private static async Task RunAsync() { var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( KeyName, Key); var uri = new Uri(string.Format("https://{0}/{1}", RelayNamespace, ConnectionName)); var token = (await tokenProvider.GetTokenAsync(uri.AbsoluteUri, TimeSpan.FromHours(1))).TokenString; var client = new HttpClient(); var request = new HttpRequestMessage() { RequestUri = uri, Method = HttpMethod.Get, }; request.Headers.Add("ServiceBusAuthorization", token); var response = await client.SendAsync(request); Console.WriteLine(await response.Content.ReadAsStringAsync()); } } }
Note
The sample code in this article uses a connection string to authenticate to an Azure Relay namespace to keep the tutorial simple. We recommend that you use Microsoft Entra ID authentication in production environments, rather than using connection strings or shared access signatures, which can be more easily compromised. For detailed information and sample code for using the Microsoft Entra ID authentication, see Authenticate and authorize an application with Microsoft Entra ID to access Azure Relay entities and Authenticate a managed identity with Microsoft Entra ID to access Azure Relay resources.
Run the applications
Run the server application. You see the following text in the console window:
Online Server listening
Run the client application. You see
hello!
in the client window. The client sent an HTTP request to the server, and server responded with ahello!
.Now, to close the console windows, press ENTER in both the console windows.
Congratulations, you've created a complete Hybrid Connections application!
Next steps
In this quickstart, you created .NET client and server applications that used HTTP to send and receive messages. The Hybrid Connections feature of Azure Relay also supports using WebSockets to send and receive messages. To learn how to use WebSockets with Azure Relay Hybrid Connections, see the WebSockets quickstart.
In this quickstart, you used .NET Framework to create client and server applications. To learn how to write client and server applications using Node.js, see the Node.js WebSockets quickstart or the Node.js HTTP quickstart.