您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 ASP.NET 和 SignalR 服务创建聊天室Quickstart: Create a chat room with ASP.NET and SignalR Service

Azure SignalR 服务基于适用于 ASP.NET Core 2.0 的 SignalR,后者并非与 ASP.NET SignalR 100% 兼容。Azure SignalR Service is based on SignalR for ASP.NET Core 2.0, which is not 100% compatible with ASP.NET SignalR. Azure SignalR 服务基于最新的 ASP.NET Core 技术重新实现了 ASP.NET SignalR 数据协议。Azure SignalR Service re-implemented ASP.NET SignalR data protocol based on the latest ASP.NET Core technologies. 使用用于 ASP.NET SignalR 的 Azure SignalR 服务时,某些 ASP.NET SignalR 功能不再受支持,例如 Azure SignalR 在客户端重新连接时不重播消息。When using Azure SignalR Service for ASP.NET SignalR, some ASP.NET SignalR features are no longer supported, for example, Azure SignalR does not replay messages when the client reconnects. 另外,Forever Frame 传输和 JSONP 也不受支持。Also, the Forever Frame transport and JSONP are not supported. 若要使 ASP.NET SignalR 应用程序兼容 SignalR 服务,必须进行一些代码更改并确保所依赖库的版本正确。Some code changes and proper version of dependent libraries are needed to make ASP.NET SignalR application work with SignalR Service.

请参阅版本差异文档,获取在 ASP.NET SignalR 和 ASP.NET Core SignalR 之间进行的功能比较的完整列表。Refer to the version differences doc for a complete list of feature comparison between ASP.NET SignalR and ASP.NET Core SignalR.

本快速入门介绍如何从 ASP.NET 和 Azure SignalR 服务着手来创建类似的聊天室应用程序In this quickstart, you will learn how to get started with the ASP.NET and Azure SignalR Service for a similar Chat Room application.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

登录 AzureSign in to Azure

使用 Azure 帐户登录到 Azure 门户Sign in to the Azure portal with your Azure account.

创建 Azure SignalR 服务实例Create an Azure SignalR Service instance

你的应用程序将连接到 Azure 中的 SignalR 服务实例。Your application will connect to a SignalR Service instance in Azure.

  1. 选择 Azure 门户左上角的“新建”按钮。Select the New button found on the upper left-hand corner of the Azure portal. 在“新建”屏幕中,在搜索框中键入“SignalR 服务” ,然后按 Enter。In the New screen, type SignalR Service in the search box and press enter.

    搜索“SignalR 服务”

  2. 从搜索结果中选择“SignalR 服务” ,然后选择“创建” 。Select SignalR Service from the search results, then select Create.

  3. 输入以下设置。Enter the following settings.

    设置Setting 建议的值Suggested value 说明Description
    资源名称Resource name 全局唯一名称Globally unique name 用于标识新的 SignalR 服务实例的名称。Name that identifies your new SignalR Service instance. 有效的字符是 a-z0-9-Valid characters are a-z, 0-9, and -.
    订阅Subscription 订阅Your subscription 在其下创建此新 SignalR 服务实例的订阅。The subscription under which this new SignalR Service instance is created.
    资源组Resource Group myResourceGroupmyResourceGroup 要在其中创建 SignalR 服务实例的新资源组的名称。Name for the new resource group in which to create your SignalR Service instance.
    位置Location 美国西部West US 选择你附近的区域Choose a region near you.
    定价层Pricing tier 免费Free 免费试用 Azure SignalR 服务。Try Azure SignalR Service for free.
    单位计数Unit count 不适用Not applicable 单位计数指定 SignalR 服务实例可以接受的连接数。Unit count specifies how many connections your SignalR Service instance can accept. 它只能在标准层中配置。It is only configurable in the Standard tier.
    服务模式Service mode 无服务器Serverless 用于 Azure Functions 或 REST API。For use with Azure Functions or REST API.

    创建 SignalR 服务

  4. 选择“创建” 以开始部署 SignalR 服务实例。Select Create to start deploying the SignalR Service instance.

  5. 部署该实例后,在门户中打开它并找到其“设置”页。After the instance is deployed, open it in the portal and locate its Settings page. 仅当通过 Azure Functions 绑定或 REST API 使用 Azure SignalR 服务时,才将“服务模式”设置更改为“无服务器” 。Change the Service Mode setting to Serverless only if you are using Azure SignalR Service through Azure Functions binding or REST API. 否则,将其保留在“经典” 或“默认” 中。Leave it in Classic or Default otherwise.

ASP.NET SignalR 应用程序不支持无服务器模式。 Serverless mode is not supported for ASP.NET SignalR applications. 对于 Azure SignalR 服务实例,请始终使用“默认”或“经典”。 Always use Default or Classic for the Azure SignalR Service instance.

也可根据创建 SignalR 服务脚本中的说明,创建在本快速入门中使用的 Azure 资源。You can also create Azure resources used in this quickstart with Create a SignalR Service script.

克隆示例应用程序Clone the sample application

在部署该服务时,让我们切换到使用代码。While the service is deploying, let's switch to working with code. 克隆来自 GitHub 的示例应用,设置 SignalR 服务连接字符串,并在本地运行该应用程序。Clone the sample app from GitHub, set the SignalR Service connection string, and run the application locally.

  1. 打开 git 终端窗口。Open a git terminal window. 切换到要克隆示例项目的文件夹。Change to a folder where you want to clone the sample project.

  2. 运行下列命令以克隆示例存储库。Run the following command to clone the sample repository. 此命令在计算机上创建示例应用程序的副本。This command creates a copy of the sample app on your computer.

    git clone https://github.com/aspnet/AzureSignalR-samples.git
    

配置并运行聊天室 Web 应用Configure and run Chat Room web app

  1. 启动 Visual Studio,并打开所克隆存储库的 aspnet-samples/ChatRoom/ 文件夹中的解决方案。Start Visual Studio and open the solution in the aspnet-samples/ChatRoom/ folder of the cloned repository.

  2. 在打开了 Azure 门户的浏览器中,查找并选择所创建的实例。In the browser where the Azure portal is opened, find and select the instance you created.

  3. 选择“密钥” 以查看 SignalR 服务实例的连接字符串。Select Keys to view the connection strings for the SignalR Service instance.

  4. 选择并复制主连接字符串。Select and copy the primary connection string.

  5. 现在请在 web.config 文件中设置连接字符串。Now set the connection string in the web.config file.

    <configuration>
    <connectionStrings>
        <add name="Azure:SignalR:ConnectionString" connectionString="<Replace By Your Connection String>"/>
    </connectionStrings>
    ...
    </configuration>
    
  6. 需在 Startup.cs 中调用 MapAzureSignalR({your_applicationName}) 而不是 MapSignalR(),传入连接字符串,使应用程序连接到服务,而不是自行托管 SignalR。In Startup.cs, instead of calling MapSignalR(), you need to call MapAzureSignalR({your_applicationName}) and pass in connection string to make the application connect to the service instead of hosting SignalR by itself. {YourApplicationName} 替换为应用程序的名称。Replace {YourApplicationName} to the name of your application. 此名称是独一无二的名称,可以将此应用程序与其他应用程序区别开来。This name is a unique name to distinguish this application from your other applications. 可以使用 this.GetType().FullName 作为值。You can use this.GetType().FullName as the value.

    public void Configuration(IAppBuilder app)
    {
        // Any connection or hub wire up and configuration should go here
        app.MapAzureSignalR(this.GetType().FullName);
    }
    

    此外还需在使用这些 API 之前参考服务 SDK。You also need to reference the service SDK before using these APIs. 打开“工具”|“NuGet 包管理器”|“包管理器控制台”,然后运行以下命令: Open the Tools | NuGet Package Manager | Package Manager Console and run command:

    Install-Package Microsoft.Azure.SignalR.AspNet
    

    除了这些更改,所有其他功能保持不变,你仍然可以使用已经熟悉的中心界面来编写业务逻辑。Other than these changes, everything else remains the same, you can still use the hub interface you're already familiar with to write business logic.

    备注

    在实现时,Azure SignalR 服务 SDK 会公开用于协商的终结点 /signalr/negotiateIn the implementation an endpoint /signalr/negotiate is exposed for negotiation by Azure SignalR Service SDK. 它会在客户端尝试连接时返回特殊的协商响应,将客户端重定向到连接字符串中定义的服务终结点。It will return a special negotiation response when clients try to connect and redirect clients to service endpoint defined in the connection string.

  7. F5 以调试模式运行项目。Press F5 to run the project in debug mode. 可以看到应用程序在本地运行。You can see the application runs locally. 它现在会连接到 Azure SignalR 服务,而不是由应用程序自身来托管 SignalR 运行时。Instead of hosting a SignalR runtime by application itself, it now connects to the Azure SignalR Service.

清理资源Clean up resources

如果不打算继续使用此应用,请按照以下步骤删除本快速入门中创建的所有资源,以免产生任何费用:If you're not going to continue to use this app, delete all resources created by this quickstart with the following steps so you don't incur any charges:

  1. 在 Azure 门户的最左侧选择“资源组”,,然后选择创建的资源组。 In the Azure portal, select Resource groups on the far left, and then select the resource group you created. 或者,可以使用搜索框按名称查找资源组。Alternatively, you may use the search box to find the resource group by its name.

  2. 在打开的窗口中选择资源组,然后单击“删除资源组”。 In the window that opens, select the resource group, and then click Delete resource group.

  3. 在新窗口中键入要删除的资源组的名称,然后单击“删除” 。In the new window, type the name of the resource group to delete, and then click Delete.

重要

删除资源组的操作不可逆,资源组以及其中的所有资源将被永久删除。Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. 请确保不会意外删除错误的资源组或资源。Make sure that you do not accidentally delete the wrong resource group or resources. 如果在现有资源组(其中包含要保留的资源)中为托管此示例而创建了相关资源,可从各自的边栏选项卡逐个删除这些资源,而不要删除资源组。If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually from their respective blades instead of deleting the resource group.

登录到 Azure 门户,并单击“资源组”。 Sign in to the Azure portal and click Resource groups.

在“按名称筛选...”文本框中键入资源组的名称 。In the Filter by name... textbox, type the name of your resource group. 本快速入门的说明使用了名为“SignalRTestResources”的资源组 。The instructions for this quickstart used a resource group named SignalRTestResources. 在结果列表中的资源组上,单击“...”,然后单击“删除资源组” 。On your resource group in the result list, click ... then Delete resource group.

删除

片刻之后,将会删除该资源组及其包含的所有资源。After a few moments, the resource group and all of its contained resources are deleted.

后续步骤Next steps

在本快速入门中,我们创建了一个新的 Azure SignalR 服务资源,并将其与 ASP.NET Web 应用配合使用。In this quickstart, you created a new Azure SignalR Service resource and used it with an ASP.NET web app. 接下来,需了解如何将 Azure SignalR 服务与 ASP.NET Core 配合使用,以便开发实时应用程序。Next, learn how to develop real-time applications using Azure SignalR Service with ASP.NET Core.