NServicebus 无法解析处理程序中接口的服务

NServicebus not unable to resolve service for interface in handler

我正在尝试构建我的第一个 NserviceBus 应用程序,但我无法让它工作。消息到达“后端”,但处理程序使用 DI,而 NServicebus 声称它找不到接口的服务。但是代码是直接从示例中提取的。我遗漏了一些东西,我无法弄明白。

错误: 错误:NServiceBus.LoadHandlersConnector[0] 处理消息类型:Messages.JobStartCommand 留言headers: NServiceBus.MessageId:0044f261-e3b3-4287-b6f0-ad7400ef43cb NServiceBus.MessageIntent : 发送 NServiceBus.ConversationId:bb4f276f-63fe-450b-b234-ad7400ef43cd NServiceBus.CorrelationId:0044f261-e3b3-4287-b6f0-ad7400ef43cb NServiceBus.ReplyToAddress:客户端界面 NServiceBus.OriginatingMachine:L19002992 NServiceBus.OriginatingEndpoint:客户端界面 $.diagnostics.originating.hostid : e1fecb2b72b8185e47341bb4dfb37dd7 NServiceBus.ContentType : text/xml NServiceBus.EnclosedMessageTypes:Messages.JobStartCommand,消息,版本=1.0.0.0,文化=中性,PublicKeyToken=null NServiceBus.Version:7.5.0 NServiceBus.TimeSent : 2021-07-29 14:31:08:228497 Z NServiceBus.Retries:3 NServiceBus.Retries.时间戳:2021-07-29 14:31:40:397277 Z 要调用的处理程序: Extractor.JobStartHandler Extractor.JobStartHandler 信息:NServiceBus.RecoverabilityExecutor[0] 由于异常,立即重试将重试消息“0044f261-e3b3-4287-b6f0-ad7400ef43cb”: System.InvalidOperationException:尝试激活 'Extractor.JobStartHandler' 时无法解析类型 'IExtract' 的服务。 在 Microsoft.Extensi

我的后端program.cs

using Microsoft.Extensions.Hosting;
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NServiceBus;
using System.Threading.Tasks;
using Messages;
using Extractor;

namespace ExtractorOsiris
{
    class Program
    {
      
        static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var builder = Host.CreateDefaultBuilder(args);
            builder.UseWindowsService();

            //builder.UseMicrosoftLogFactoryLogging();
            builder.ConfigureLogging((ctx, logging) =>
            {
                logging.AddConfiguration(ctx.Configuration.GetSection("Logging"));

                //logging.AddEventLog();
                logging.AddConsole();
                logging.SetMinimumLevel(LogLevel.Debug);
            });

            #region back-end-use-nservicebus

            builder.UseNServiceBus(ctx =>
            {
                var endpointConfiguration = new EndpointConfiguration("Sample.Extractor");
                endpointConfiguration.UseTransport<LearningTransport>();

                var transport = endpointConfiguration.UseTransport<LearningTransport>();
                transport.Routing().RouteToEndpoint(typeof(ProcessObjectCommand), "Sample.Processor");

                endpointConfiguration.DefineCriticalErrorAction(OnCriticalError);

                return endpointConfiguration;
            });

            #endregion

            #region back-end-register-service
            
            builder.ConfigureServices(services =>
            {
                services.AddSingleton<IExtract, ExtractOsiris>();
            });

           
            #endregion



            return builder;

        }

        private static Task OnCriticalError(ICriticalErrorContext arg)
        {
            throw new NotImplementedException();
        }
    }
}

界面

using Newtonsoft.Json.Linq;
using NServiceBus;
using System;
using System.Threading.Tasks;

namespace Extractor
{
    public interface IExtract
    {
        Task<JArray> Extract(string @object, DateTime deltaTime);
    }
}

处理程序

using System.Threading.Tasks;
using Messages;
using Newtonsoft.Json.Linq;
using NServiceBus;


namespace Extractor
{
    #region back-end-handler
    public class JobStartHandler : IHandleMessages<JobStartCommand>
    {
        private readonly IExtract extractor;

        public JobStartHandler(IExtract extractor)
        {
            this.extractor = extractor;
        }

        public async Task Handle(JobStartCommand message, IMessageHandlerContext context)
        {
            Task<JArray> result = extractor.Extract("Medewerkers", message.DeltaTime);
            await result;

            JArray test = result.Result;

            foreach (JObject x in test)
            {
                // send the object to be processed.
                //await context.Send(new ProcessObjectCommand(x.ToString()));
            }
        }
    }
    #endregion

}

接口的实现

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Extractor;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NServiceBus;


namespace ExtractorOsiris
{
    public class ExtractOsiris : IExtract
    {
        private readonly ILogger logger;
       
        public ExtractOsiris(ILogger<ExtractOsiris> logger)
        {
            this.logger = logger; 
        }

        public Task<JArray> Extract(string @object, DateTime deltaTime)
        {
            logger.LogInformation($"getting {@object} for delta time {deltaTime}");

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://xx.yy");
            

            WebResponse response = request.GetResponse();

            JObject temp = JObject.Load(new JsonTextReader(new StreamReader(response.GetResponseStream())));



            return Task<JArray>.FromResult(temp["items"] as JArray);
        }
    }
}

我知道代码不漂亮,但它是纯粹的快速和肮脏的测试代码。

我从代码中清除了有问题的接口,但仍然在一个甚至不再存在的接口上出现错误。我删除了 bin 文件夹并逐步备份所有内容。愚蠢的是,源代码管理说我回到了我开始的地方,没有任何变化。而且它确实有效....

所以 VS2019 一定有一些愚蠢的东西。对浪费我时间的人表示抱歉