gpt4 book ai didi

c# - 将数据存储在 Azure WebJob 中

转载 作者:行者123 更新时间:2023-12-03 02:00:02 25 4
gpt4 key购买 nike

我创建了一个从服务器接收数据的 gRPC 客户端。客户端作为 Azure WebJob 运行。

接收到的数据看起来每条消息都有一个重放 ID。我目前将此重播 ID 作为文件存储在本地。收到的重播 ID 会附加到文件中。需要重播 ID,以便在出现问题时可以从该 ID 继续。

如果我在本地运行客户端,这不是问题,但我认为作为 WebJob 并不理想。我当前的问题是,在运行时创建/修改的文件存储在这里:

C:\local\Temp\jobs\continuous\SalesforceIngestor.App\ev4ygzfw.ezh

如果 WebJob 由于任何原因重新启动,replayIdStorage.txt 文件中的数据将会丢失。

是否有任何方法可以安全地将数据存储在 WebJob 中,以便重新启动后可以检索数据?

或者 Azure Blob 存储是更好的选择吗?

最佳答案

我尝试了以下 .Net Web 作业代码,并且能够将 Web 应用程序的 Web 作业数据存储在我的存储帐户容器中。

代码:

Program.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace WebJob1
{
internal class Program
{
static void Main()
{
var config = new JobHostConfiguration();

if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}

var host = new JobHost(config);
host.RunAndBlock();
}
}
}

函数.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;

namespace WebJob1
{
public class Functions
{
private const string ConnectionStringName = "AzureWebJobsStorage";

public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
{
log.WriteLine(message);
string replayId = GenerateReplayIdFromMessage(message);
StoreReplayIdInBlobStorage(replayId);
}

private static string GenerateReplayIdFromMessage(string message)
{
return Guid.NewGuid().ToString();
}

private static void StoreReplayIdInBlobStorage(string replayId)
{
var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable(ConnectionStringName));
var blobClient = storageAccount.CreateCloudBlobClient();
var containerName = "replayids";
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();

var blobName = "replayIdStorage.txt";
var blob = container.GetBlockBlobReference(blobName);
blob.UploadText(replayId);
}
}
}

应用程序配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<!-- The format of the connection string is "DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY" -->
<!-- For local execution, the value can be set either in this config file or through environment variables -->
<add name="AzureWebJobsDashboard" connectionString="<storage_connec_string>" />
<add name="AzureWebJobsStorage" connectionString="<storage_connec_string>" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

输出:

运行成功如下,

enter image description here

我在 Azure 门户的存储帐户中创建了两个容器,如下所示,

enter image description here

我在azure-webjobs-hosts容器中获得了Id,如下所示,

enter image description here

并在azure-jobs-host-output容器中,

enter image description here

enter image description here

然后,我将我的代码发布网络应用,如下所示,

enter image description here

它已成功发布Azure Portal网络应用中的网络作业。然后,我单击运行开始运行网络作业,如下所示,

enter image description here

Web 作业运行已成功启动,如下所示,

enter image description here

enter image description here

然后,我在我的存储帐户azure-jobs-host-output容器中存储了网络应用详细信息,如下所示,

enter image description here

关于c# - 将数据存储在 Azure WebJob 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76613629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com