开始在 .NET 中使用中继混合连接 HTTP 请求Get started with Relay Hybrid Connections HTTP requests in .NET

在本快速入门中,请创建 .NET 发送者和接收者应用程序,用于通过 HTTP 协议发送和接收消息。In this quickstart, you create .NET sender and receiver applications that send and receive messages by using the HTTP protocol. 这些应用程序使用 Azure 中继的混合连接功能。The applications use Hybrid Connections feature of Azure Relay. 若要了解 Azure 中继的常规信息,请参阅 Azure 中继To learn about Azure Relay in general, see Azure Relay.

在本快速入门中,你将执行以下步骤:In this quickstart, you take the following steps:

  1. 使用 Azure 门户创建中继命名空间。Create a Relay namespace by using the Azure portal.
  2. 使用 Azure 门户在该命名空间中创建混合连接。Create a hybrid connection in that namespace by using the Azure portal.
  3. 编写服务器(侦听器)控制台应用程序,用于接收消息。Write a server (listener) console application to receive messages.
  4. 编写客户端(发送方)控制台应用程序,用于发送消息。Write a client (sender) console application to send messages.
  5. 运行应用程序。Run applications.

先决条件Prerequisites

若要完成本教程,需要满足以下先决条件:To complete this tutorial, you need the following prerequisites:

1.创建命名空间1. Create a namespace

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左侧菜单中,选择“+ 创建资源”。In the left menu, select + Create a resource. 然后,选择“企业集成” > “中继”。Then, select Enterprise Integration > Relay.

  3. 在“创建命名空间”下输入命名空间名称。Under Create namespace, enter a namespace name. 系统会立即检查该名称是否可用。The system immediately checks to see if the name is available.

  4. 在“订阅”框中,选择要在其中创建命名空间的 Azure 订阅。In the Subscription box, select an Azure subscription in which to create the namespace.

  5. “资源组”框中,选择要在其中放置命名空间的现有资源组,或者创建新资源组。In the Resource group box, select an existing resource group in which to place the namespace, or create a new one.

  6. 在“位置”中,选择应托管命名空间的国家或地区。In Location, select the country or region in which your namespace should be hosted.

    创建命名空间

  7. 选择“创建” 。Select Create. 系统会创建并启用命名空间。The system creates your namespace and enables it. 几分钟后,系统为用户的帐户预配资源。After a few minutes, the system provisions resources for your account.

获取管理凭据Get management credentials

  1. 选择“所有资源”,然后选择新创建的命名空间名称。Select All resources, and then select the newly created namespace name.

  2. 在“中继命名空间”下,选择“共享访问策略”。Under the Relay namespace, select Shared access policies.

  3. 在“共享访问策略”下,选择“RootManageSharedAccessKey”。Under Shared access policies, select RootManageSharedAccessKey.

    connection-info

  4. 在“策略: RootManageSharedAccessKey”下选择“连接字符串 - 主密钥”旁边的“复制”按钮。Under Policy: RootManageSharedAccessKey, select the Copy button next to Connection string–Primary key. 这样会将连接字符串复制到剪贴板,供以后使用。This copies the connection string to your clipboard for later use. 将此值粘贴到记事本或其他某个临时位置。Paste this value into Notepad or some other temporary location.

    连接字符串

  5. 重复上述步骤,将主密钥的值复制和粘贴到临时位置,供以后使用。Repeat the preceding step to copy and paste the value of Primary key to a temporary location for later use.

2.创建混合连接2. Create a hybrid connection

确保已创建中继命名空间Ensure that you have already created a Relay namespace.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左侧菜单中,选择“所有资源”。In the left menu, select All resources.

  3. 选择要在其中创建混合连接的命名空间。Select the namespace where you want to create the hybrid connection. 在本示例中,该命名空间为 mynewnsIn this case, it is mynewns.

  4. 在“中继命名空间”下选择“混合连接”。Under Relay namespace, select Hybrid Connections.

    创建混合连接

  5. 在命名空间概览窗口中,选择“+ 混合连接”In the namespace overview window, select + Hybrid Connection

    选择混合连接

  6. 在“创建混合连接”下输入一个值,作为混合连接名称。Under Create Hybrid Connection, enter a value for the hybrid connection name. 保留其他默认值。Leave the other default values.

    选择“新建”

  7. 选择“创建” 。Select Create.

3.创建服务器应用程序(侦听程序)3. Create a server application (listener)

在 Visual Studio 中编写可侦听和接收来自中继的消息的 C# 控制台应用程序。In Visual Studio, write a C# console application to listen for and receive messages from the relay.

创建控制台应用程序Create a console application

在 Visual Studio 中创建新的控制台应用 (.NET Framework) 项目。In Visual Studio, create a new Console App (.NET Framework) project.

添加中继 NuGet 包Add the Relay NuGet package

  1. 右键单击新创建的项目,然后选择“管理 NuGet 包”。Right-click the newly created project, and then select Manage NuGet Packages.
  2. 选择“浏览”,然后搜索 Microsoft.Azure.RelaySelect Browse, and then search for Microsoft.Azure.Relay. 在搜索结果中,选择“Microsoft Azure 中继”。In the search results, select Microsoft Azure Relay.
  3. 选择“安装”即可完成安装。Select Install to complete the installation. 关闭对话框。Close the dialog box.

编写接收消息的代码Write code to receive messages

  1. 将 Program.cs 文件顶部的现有 using 语句替换为以下 using 语句:At the top of the Program.cs file, replace the existing using statements with the following using statements:

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.Relay;
    
  2. 将常量添加到 Program 类,用于保存混合连接的连接详细信息。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.

    private const string RelayNamespace = "{RelayNamespace}.servicebus.windows.net";
    private const string ConnectionName = "{HybridConnectionName}";
    private const string KeyName = "{SASKeyName}";
    private const string Key = "{SASKey}";
    
  3. RunAsync 方法添加到 Program 类:Add the RunAsync method to the Program 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();
    }
    
  4. Program 类的 Main 方法中添加以下代码行:Add the following line of code to the Main method in the Program class:

    RunAsync().GetAwaiter().GetResult();
    

    完成的 Program.cs 文件应如下所示: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;
    
        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();
            }
        }
    }
    

4.创建客户端应用程序(发送程序)4. Create a client application (sender)

在 Visual Studio 中编写可将消息发送到中继的 C# 控制台应用程序。In Visual Studio, write a C# console application to send messages to the relay.

创建控制台应用程序Create a console application

如果创建中继时已禁用“需要客户端授权”选项,可使用任何浏览器向混合连接 URL 发送请求。If you have disabled the "Requires Client Authorization" option when creating the Relay, you can send requests to the Hybrid Connections URL with any browser. 若要访问受保护的终结点,需在 ServiceBusAuthorization 标头中创建并传递令牌,如下所示。For accessing protected endpoints, you need to create and pass a token in the ServiceBusAuthorization header, which is shown here.

在 Visual Studio 中创建新的控制台应用 (.NET Framework) 项目。In Visual Studio, create a new Console App (.NET Framework) project.

添加中继 NuGet 包Add the Relay NuGet package

  1. 右键单击新创建的项目,然后选择“管理 NuGet 包”。Right-click the newly created project, and then select Manage NuGet Packages.
  2. 选择“浏览”,然后搜索 Microsoft.Azure.RelaySelect Browse, and then search for Microsoft.Azure.Relay. 在搜索结果中,选择“Microsoft Azure 中继”。In the search results, select Microsoft Azure Relay.
  3. 选择“安装”即可完成安装。Select Install to complete the installation. 关闭对话框。Close the dialog box.

编写代码以发送请求Write code to send requests

  1. 将 Program.cs 文件顶部的现有 using 语句替换为以下 using 语句:At the top of the Program.cs file, replace the existing using statements with the following using statements:

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Net.Http;
    using Microsoft.Azure.Relay;
    
  2. 将常量添加到 Program 类,用于保存混合连接的连接详细信息。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.

    private const string RelayNamespace = "{RelayNamespace}.servicebus.windows.net";
    private const string ConnectionName = "{HybridConnectionName}";
    private const string KeyName = "{SASKeyName}";
    private const string Key = "{SASKey}";
    
  3. 将以下方法添加到 Program 类: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();
    }
    
  4. Program 类的 Main 方法中添加以下代码行。Add the following line of code to the Main method in the Program class.

    RunAsync().GetAwaiter().GetResult();
    

    Program.cs 应如下所示:The Program.cs should look like this:

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.Relay;
    
    namespace Client
    {
        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}";
    
            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());
            }
        }
    }
    

5.运行应用程序5. Run the applications

  1. 运行服务器应用程序。Run the server application. 可以在控制台窗口中看到以下文本:You see the following text in the console window:

    Online
    Server listening
    
  2. 运行客户端应用程序。Run the client application. 你将在客户端窗口中看到 hello!You see hello! in the client window. 客户端向服务器发送了一个 HTTP 请求,服务器以 hello! 进行响应。The client sent an HTTP request to the server, and server responded with a hello!.

  3. 现在,若要关闭控制台窗口,请在两个控制台窗口中按 ENTERNow, to close the console windows, press ENTER in both the console windows.

祝贺你,你已创建了一个完整的混合连接应用程序!Congratulations, you've created a complete Hybrid Connections application!

后续步骤Next steps

在本快速入门中,你创建了 .NET 客户端和服务器应用程序,此类程序使用 HTTP 来发送和接收消息。In this quickstart, you created .NET client and server applications that used HTTP to send and receive messages. Azure 中继的混合连接功能也支持使用 WebSocket 发送和接收消息。The Hybrid Connections feature of Azure Relay also supports using WebSockets to send and receive messages. 若要了解如何将 WebSocket 与 Azure 中继混合连接配合使用,请参阅 WebSocket 快速入门To learn how to use WebSockets with Azure Relay Hybrid Connections, see the WebSockets quickstart.

在本快速入门中,你使用了 .NET Framework 来创建客户端和服务器应用程序。In this quickstart, you used .NET Framework to create client and server applications. 若要了解如何使用 Node.js 编写客户端和服务器应用程序,请参阅 Node.js WebSocket 快速入门Node.js HTTP 快速入门To learn how to write client and server applications using Node.js, see the Node.js WebSockets quickstart or the Node.js HTTP quickstart.