| author | clemensv |
|---|---|
| ms.service | azure-relay |
| ms.topic | include |
| ms.date | 01/24/2026 |
| ms.author | samurp |
In Visual Studio, create a new Console App (.NET Framework) project.
- 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.
-
At the top of the Program.cs file, replace the existing
usingstatements with the followingusingstatements:using System; using System.IO; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Relay; using System.Net;
-
Add constants to the
Programclass 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.windows.net"; // 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
RunAsyncmethod to theProgramclass: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
Mainmethod in theProgramclass: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.windows.net"; 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(); } } }