简体   繁体   English

从 Azure 服务总线主题读取的 Asp.net Core 2.2

[英]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.

相关问题 从 ASP.NET Core 2.0 控制台应用程序监控 Azure 服务总线队列深度 - Monitoring Azure Service Bus Queue Depth from ASP.NET Core 2.0 Console App Asp.net样板和Azure服务总线 - Asp.net Boilerplate and Azure Service Bus 如何从 .net 核心应用程序向服务总线主题发送消息 - How to send a message to service bus topic from .net core application MassTransit Azure 服务总线请求-响应 ASP.NET 核心 RequestTimeoutException - MassTransit Azure Service Bus Request-Response ASP.NET Core RequestTimeoutException 依赖注入不适用于带有 Azure Key Vault Value asp.net core 3.0 的 RabitMQ 服务总线 - Dependency Injection not working with RabitMQ Service Bus with Azure Key Vault Value asp.net core 3.0 Azure上托管的ASP.NET Core 2.2 App Service返回500,没有引发异常 - ASP.NET Core 2.2 App Service hosted on Azure returning 500 without an exception being thrown ASP.NET Core 2.2 中的 Azure AD 身份验证 - Azure AD Authentication in ASP.NET Core 2.2 使用服务层验证 asp.net core mvc 2.2 - asp.net core mvc 2.2 validating with a service layer ASP.NET Core 2.2 集成测试 DbContext 服务未注册 - ASP.NET Core 2.2 Integration Testing DbContext Service not registered 从2.1-&gt; 2.2的ASP.NET Core迁移-Azure部署中的依赖关系问题 - ASP.NET Core Migration from 2.1 -> 2.2 Issue with dependencies in Azure deployment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM