Azure 如何使用webjobs在文档数据库中推送数据?
我已经编写了以下代码来读取文本文件中的数据,然后分离句子并将它们发送到azure事件中心 我能够将数据发送到事件中心,但无法将数据推送到文档数据库中 如何使用webjobs在文档数据库中推送数据?我正在从控制台应用程序运行webjobs,是否需要添加更多配置以在本地运行它? Program.csAzure 如何使用webjobs在文档数据库中推送数据?,azure,azure-storage,azure-webjobs,Azure,Azure Storage,Azure Webjobs,我已经编写了以下代码来读取文本文件中的数据,然后分离句子并将它们发送到azure事件中心 我能够将数据发送到事件中心,但无法将数据推送到文档数据库中 如何使用webjobs在文档数据库中推送数据?我正在从控制台应用程序运行webjobs,是否需要添加更多配置以在本地运行它? Program.cs static void Main(string[] args) { JobHostConfiguration config = new JobHostCon
static void Main(string[] args)
{
JobHostConfiguration config = new JobHostConfiguration();
config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Error;
var eventHubConfig = new EventHubConfiguration();
eventHubConfig.AddReceiver(eventHubName, connectionString);
config.UseEventHub(eventHubConfig);
JobHost host = new JobHost(config);
config.DashboardConnectionString = StorageConnectionString;
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
//Send test messages
Task.Run(() =>
{
SendMessagesToEventHub();
});
host.RunAndBlock();
}
function.cs
class Functions
{
public static void Run([EventHubTrigger("azurepochub")] EventData message, [Microsoft.Azure.WebJobs.DocumentDB("testcosmosdb01122018", "Items", ConnectionStringSetting = "dbConnctionString")]out dynamic document)
{
string data = Encoding.UTF8.GetString(message.GetBytes());
document = data;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Message received. Data: '{data}'");
Console.ResetColor();
}
}
您可以从github获得答案。如果我们想将文档插入Azure documentdb,我们需要将对象插入其中。在您的例子中,您的输出是字符串 我是否需要添加更多配置以在本地运行它 我也为此做了演示。以下是详细的步骤 1.创建.net framework Webjob项目 2.在App.config文件中添加AzureWebJobsDocumentDBConnectionString
<appSettings>
<!-- Service Bus specific app setings for messaging connections -->
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]" />
<add key ="AzureWebJobsDocumentDBConnectionString" value="xxxxx"/>
</appSettings>
4.在function.cs中
public static void Run([EventHubTrigger("eventhub name")] EventData message, [DocumentDB("document Database name", "collection", ConnectionStringSetting = "AzureWebJobsDocumentDBConnectionString")]out Item document)
{
string data = Encoding.UTF8.GetString(message.GetBytes());
document = new Item{Text = data};
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Message received. Data: '{data}'");
Console.ResetColor();
}
public class Item
{
public string Text;
}
5.从控制台检查结果
“我已经编写了以下代码…”它在哪里?我已经添加了代码您有关于此线程的任何更新吗?
public static void Run([EventHubTrigger("eventhub name")] EventData message, [DocumentDB("document Database name", "collection", ConnectionStringSetting = "AzureWebJobsDocumentDBConnectionString")]out Item document)
{
string data = Encoding.UTF8.GetString(message.GetBytes());
document = new Item{Text = data};
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Message received. Data: '{data}'");
Console.ResetColor();
}
public class Item
{
public string Text;
}