You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure WebJobs SDK中IUniformSession依赖注入解析为null求助

问题:Azure WebJobs中IUniformSession注入始终为null

环境版本

  • .NET 6
  • NServiceBus 7.7.4
  • NServiceBus.Extensions.Hosting 1.1.0
  • NServiceBus.UniformSession 2.2.0
  • Microsoft.Azure.WebJobs 3.0.33

问题描述

在基于Generic Host和.NET内置DI的Azure WebJobs SDK项目中,尝试通过构造函数将IUniformSession注入到Job类,但该实例始终为null。已确认DI容器中已注册该服务,但实际解析时返回null。

复现代码

Program.cs

using Microsoft.Extensions.Hosting;
using NServiceBus;
using NServiceBus.UniformSession;

var host = new HostBuilder()
    .UseWebJobs()
    .ConfigureServices(services =>
    {
        services.AddUniformSession();
        services.AddNServiceBus(context =>
        {
            var endpointConfiguration = new EndpointConfiguration("WebJobsEndpoint");
            endpointConfiguration.UseTransport<LearningTransport>();
            endpointConfiguration.UsePersistence<LearningPersistence>();
            return endpointConfiguration;
        });
    })
    .Build();

await host.RunAsync();

SampleJob.cs

using Microsoft.Azure.WebJobs;
using NServiceBus.UniformSession;

public class SampleJob
{
    private readonly IUniformSession _session;

    public SampleJob(IUniformSession session)
    {
        _session = session; // 此处session始终为null
    }

    [FunctionName("SampleTrigger")]
    public async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer)
    {
        if (_session == null)
        {
            throw new InvalidOperationException("IUniformSession is null");
        }
        await _session.Send(new SampleMessage());
    }
}

public class SampleMessage : IMessage { }

解决方法

1. 调整Uniform Session的DI注册范围

WebJobs的函数实例采用Scoped生命周期,默认的AddUniformSession()注册可能未匹配该范围。在ConfigureServices中添加Scoped注册,确保WebJobs能正确解析:

services.AddUniformSession();
// 显式注册为Scoped,适配WebJobs的激活模型
services.AddScoped<IUniformSession>(provider => provider.GetRequiredService<IUniformSession>());

2. 使用NServiceBus.WebJobs专用集成包

NServiceBus提供了针对WebJobs的官方集成包,可自动处理Uniform Session的DI适配:

  • 安装包:dotnet add package NServiceBus.WebJobs
  • 修改Program.cs配置:
var host = new HostBuilder()
    .UseWebJobs()
    .ConfigureServices(services =>
    {
        services.AddNServiceBusWebJobs(context =>
        {
            var endpointConfiguration = new EndpointConfiguration("WebJobsEndpoint");
            endpointConfiguration.UseTransport<LearningTransport>();
            endpointConfiguration.UsePersistence<LearningPersistence>();
            return endpointConfiguration;
        });
    })
    .Build();

3. 检查自定义激活器(若有)

如果项目使用了自定义WebJobs激活器,需确保其能正确从DI容器中解析所有依赖,包括IUniformSession,避免手动实例化Job类导致依赖注入失效。

4. 验证端点启动状态

在Host启动后,验证根容器中是否能获取到IUniformSession,确认NServiceBus端点已正常启动:

var session = host.Services.GetService<IUniformSession>();
Console.WriteLine(session == null ? "IUniformSession未在根容器注册" : "IUniformSession已在根容器注册");

内容的提问来源于stack exchange,提问作者Jarrich Van de Voorde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:44:16