[英]Asp.net Core 2.2 read from Azure Service Bus Topic
Good day i am writing an applicaiton that needs to read data from an Azure service bus topic.美好的一天,我正在编写一个需要从 Azure 服务总线主题读取数据的应用程序。 The registration function looks like this(see below).
注册函数看起来像这样(见下文)。 I would like to run this once on startup of The Asp.net Core application.
我想在 Asp.net Core 应用程序启动时运行一次。
What would a good way to call this function since it seems IStartupFilter does not support async functions?既然 IStartupFilter 似乎不支持异步函数,那么调用这个函数的好方法是什么?
Any help would be greatly appreciated任何帮助将不胜感激
async Task RegisterForMessages()
{
subscriptionClient = new SubscriptionClient(constring, TopicName, SubscriptionName);
// Register subscription message handler and receive messages in a loop
RegisterOnMessageHandlerAndReceiveMessages();
await subscriptionClient.CloseAsync();
}
Have you tried creating interface base approach for registering Async function like below:您是否尝试过创建用于注册异步函数的接口基本方法,如下所示:
public interface IStartupAction
{
Task ExecuteAsync(CancellationToken cancellationToken = default);
}
And a method for registering startup tasks with the DI container:以及向DI容器注册启动任务的方法:
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddStartupTask<T>(this IServiceCollection services)
where T : class, IStartupTask
=> services.AddTransient<IStartupAction, T>();
}
Finally, we add an extension method that finds all the registered IStartupAction
on app startup, runs them in order, and then starts the IWebHost
:最后,我们添加一个扩展方法,
IStartupAction
在应用程序启动时查找所有已注册的IStartupAction
,并按顺序运行它们,然后启动IWebHost
:
public static class StartupTaskWebHostExtensions
{
public static async Task RunWithTasksAsync(this IWebHost webHost, CancellationToken cancellationToken = default)
{
// Load all tasks from DI
var startupTasks = webHost.Services.GetServices<IStartupAction>();
// Execute all the tasks
foreach (var startupTask in startupTasks)
{
await startupTask.ExecuteAsync(cancellationToken);
}
// Start the tasks as normal
await webHost.RunAsync(cancellationToken);
}
}
For detailed steps , you can refer this doc.有关详细步骤,您可以参考此文档。
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.