我制作了一个 Blazor 服务器端项目,我想将其托管在 Azure 上。
它使用 CosmosDB NoSQL。
需要进行哪些更改才能托管? (如 appsettings.json 等)
我可以在我的主 PC 上毫无问题地打开它,只需登录 Azure CLI 然后运行即可。另一方面,我的 Azure 链接仍然显示空白页面。
托管设置为 github 存储库(主分支)。
澄清一下:我用 vscode 做了这个,但是在使用 Azure 门户上的 Github 存储库托管它之后,它不起作用,评论说尝试使用 Visual Studio 工具托管它,我做了,没有什么区别,但没有可见错误,只是一个空的网站。我修改了appsettings.json和launchsettings,并添加了appsettings.Production.json以及我在网上找到的必要数据,仍然没有区别,如果需要更多信息,我会提供它
我使用 Azure Cosmos DB 创建了一个示例 Blazor 服务器端应用程序,并使用 GitHub Actions 将其部署到 Linux 上的 Azure Web 应用程序。
我将以下代码行添加到我的
Program.cs
类中以配置 Azure Cosmos DB。
builder.Services.Configure<CosmosDbSettings>(builder.Configuration.GetSection("CosmosDb"));
builder.Services.AddSingleton<CosmosClient>(sp =>
{
var cosmosDbSettings = sp.GetRequiredService<IOptions<CosmosDbSettings>>().Value;
return new CosmosClient(cosmosDbSettings.AccountEndpoint, cosmosDbSettings.AccountKey);
});
builder.Services.AddSingleton<CosmosDbService<TodoItem>>(sp =>
{
var cosmosDbSettings = sp.GetRequiredService<IOptions<CosmosDbSettings>>().Value;
var cosmosClient = sp.GetRequiredService<CosmosClient>();
return new CosmosDbService<TodoItem>(cosmosClient, cosmosDbSettings.DatabaseName, cosmosDbSettings.ContainerName);
});
appsettings.son:
"CosmosDb": {
"AccountEndpoint": "https://<AzureCosmosDBAccountNamer>.documents.azure.com:443/",
"AccountKey": "<AccountKey>",
"DatabaseName": "<DatabaseName>",
"ContainerName": "ContainerName"
}
通过使用下面的工作流程文件,我成功地将应用程序部署到 Azure。
GitHub 工作流程文件:
name: Build and deploy ASP.Net Core app to Azure Web App - kablazorcosmosdb
on:
push:
branches:
- master
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v4
with:
name: .net-app
path: ${{env.DOTNET_ROOT}}/myapp
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
permissions:
id-token: write
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: .net-app
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_BE1BFEF934C747789370FA9529B537DF }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_A658578A15294EE0B10C41EFC4A32775 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_A16B6C99691C4997ABDD5AE882FCC4F7 }}
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v3
with:
app-name: 'kablazorcosmosdb'
slot-name: 'Production'
package: .
必须对 Blazor 项目进行哪些更改才能使其可托管?
使用 Azure Cosmos DB 托管 Blazor 应用程序所需的更改。
appsettings.json
值添加到 Azure Web 应用程序的环境变量部分。"AccountEndpoint": "https://<AzureCosmosDBAccountNamer>.documents.azure.com:443/",
"AccountKey": "<AccountKey>",
"DatabaseName": "<DatabaseName>",
"ContainerName": "ContainerName"
Configuration
部分。dotnet <ProjectName>.dll
-确保将正确的 IP 地址添加到
Firewall
设置中 Selected networks
下的 Networking
部分。
我正在从 Azure Cosmos DB 获取数据。
Azure 输出: