为长篇致歉!
我正在尝试在aspnet core 2.1中为RabbitMQ构建消息侦听器。发布消息后,我立即在日志中收到此错误:
2018-09-29 12:35:35.459信息NServiceBus.RecoverabilityExecutor立即重试将由于异常而重试消息'ab43':
System.InvalidOperationException:无法解析类型:Event.Processor.Listener.CustomerReceivedHandler,服务名称:---> System.InvalidOperationException:未解决的依赖项[目标类型:Event.Processor.Listener.CustomerReceivedHandler],[参数:DbProvider(事件。 Processor.Listener.IDbProvider)],[请求的依赖项:ServiceType:Event.Processor.Listener.IDbProvider,ServiceName:]
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3443中的LightInject.ServiceContainer.GetEmitMethodForDependency(Dependencydependent)
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3402中的LightInject.ServiceContainer.EmitConstructorDependency(IEmitter发射器,依赖项依赖)中
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ Object \ Builder \ LightBuilder \ LightInject \ LightInject.g.cs:行3329处的LightInject.ServiceContainer.EmitConstructorDependencies(ConstructionInfo constructionInfo,IEmitter发射器,Action 1 decoratorTargetEmitter) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\ObjectBuilder\LightInject\LightInject.g.cs:line 3368
at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, ConstructionInfo constructionInfo, Action
1 decoratorTargetEmitMethod)处
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3286中的LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration,IEmitter发射器)处
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3868中的LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action 1 serviceEmitter) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\ObjectBuilder\LightInject\LightInject.g.cs:line 3030
at LightInject.ServiceContainer.CreateInstanceDelegateIndex(Action
1发射方法)处
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3030中的LightInject.ServiceContainer.EmitLifetime(ServiceRegistration serviceRegistration,Action 1 emitMethod, IEmitter emitter) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\ObjectBuilder\LightInject\LightInject.g.cs:line 3857
at LightInject.ServiceContainer.<>c__DisplayClass139_0.<CreateEmitMethodWrapper>b__0(IEmitter ms) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\ObjectBuilder\LightInject\LightInject.g.cs:line 3109
at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action
1 serviceEmitter)处
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3915中的LightInject.ServiceContainer.CreateDelegate(Type serviceType,String serviceName,Boolean throwError)中
---内部异常堆栈跟踪的结尾---
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3920中的LightInject.ServiceContainer.CreateDelegate(Type serviceType,String serviceName,Boolean throwError)中
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 3878中的LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType,Boolean throwError)中
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ ObjectBuilder \ LightInject \ LightInject.g.cs:line 2740中的LightInject.ServiceContainer.GetInstance(Type serviceType)
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ Pipeline \ Incoming \ LoadHandlersConnector.cs:line 45的LightInject.Scope.WithThisScope [T](Func 1 function) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\ObjectBuilder\LightInject\LightInject.g.cs:line 6220
at NServiceBus.LoadHandlersConnector.Invoke(IIncomingLogicalMessageContext context, Func
2阶段)处
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ Pipeline \ Incoming \ DeserializeLogicalMessagesConnector中的NServiceBus.MutateIncomingMessageBehavior.InvokeIncomingMessageMutators(IIncomingLogicalMessageContext context,Func 2 next) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\MessageMutators\MutateInstanceMessage\MutateIncomingMessageBehavior.cs:line 60
at NServiceBus.DeserializeLogicalMessagesConnector.Invoke(IIncomingPhysicalMessageContext context, Func
2 stage)中。
在NServiceBus.UnitOfWorkBehavior.InvokeUnitsOfWork(IIncomingPhysicalMessageContext上下文,Func 2 next) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\UnitOfWork\UnitOfWorkBehavior.cs:line 40
at NServiceBus.UnitOfWorkBehavior.InvokeUnitsOfWork(IIncomingPhysicalMessageContext context, Func
2下)中的C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ UnitOfWork \ UnitOfWorkBehavior.cs:line 62
在NServiceBus.MutateIncomingTransportMessageBehavior.InvokeIncomingTransportMessagesMutators(IIncomingPhysicalMessageContext上下文,Func 2 next) in C:\BuildAgent\work\b549d46003942065\src\NServiceBus.Core\MessageMutators\MutateTransportMessage\MutateIncomingTransportMessageBehavior.cs:line 59
at NServiceBus.ProcessingStatisticsBehavior.Invoke(IIncomingPhysicalMessageContext context, Func
2下)中的C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ Performance \ Statistics \ ProcessingStatisticsBline
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ Pipeline \ Incoming \ TransportReceiveToPhysicalMessageProcessingConnector.cs:line 39处的NServiceBus.TransportReceiveToPhysicalMessageProcessingConnector.Invoke(ITransportReceiveContext上下文,Func`2下一个)处
在C:\ BuildAgent \ work \ b549d46003942065 \ src \ NServiceBus.Core \ Pipeline \ MainPipelineExecutor.cs:line 34的NServiceBus.MainPipelineExecutor.Invoke(MessageContext messageContext)处
在C:\ BuildAgent \ work \ 15dcdbe5f88af6ed \ src \ NServiceBus.Transport.RabbitMQ \ Receiving \ MessagePump.cs:line 249
csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>Event.Processor</AssemblyName>
<RootNamespace>Event.Processor</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Customer.Models" Version="1.0.21875" />
<PackageReference Include="lightinject" Version="5.2.0" />
<PackageReference Include="LightInject.Microsoft.DependencyInjection" Version="2.0.8" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="nservicebus" Version="7.1.4" />
<PackageReference Include="NServiceBus.RabbitMQ" Version="5.0.1" />
<PackageReference Include="odp.netcore" Version="2.0.12" />
<PackageReference Include="serilog" Version="2.7.1" />
<PackageReference Include="Serilog.aspnetcore" Version="2.1.1" />
<PackageReference Include="serilog.settings.configuration" Version="2.6.1" />
<PackageReference Include="serilog.sinks.console" Version="3.1.1" />
<PackageReference Include="serilog.sinks.file" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.Run(async context =>
{
await context.Response.WriteAsync("Hello Processor");
});
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
ServiceContainer container = new ServiceContainer(new ContainerOptions
{
EnablePropertyInjection = false
});
//The below didn't work either!
//services.AddSingleton<IDbProvider, DbProvider>();
//services.AddSingleton<IConfigSettings, ConfigSettings>();
//services.AddSingleton<IEncryptor, Encryptor>();
container.Register<IDbProvider, DbProvider>();
container.Register<IConfigSettings, ConfigSettings>();
container.Register<IEncryptor, Encryptor>();
return container.CreateServiceProvider(services);
}
}
Program.cs
public class Program
{
public static void Main(string[] args)
try
{
var endpointConfiguration = new EndpointConfiguration("MSG_QUEUE");
var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
transport.UseConventionalRoutingTopology();
transport.ConnectionString("ConxnString");
endpointConfiguration.EnableInstallers();
endpointConfiguration.SendFailedMessagesTo("error");
endpointConfiguration.AutoSubscribe();
endpointConfiguration.UsePersistence<InMemoryPersistence>();
endpointConfiguration.UseSerialization<XmlSerializer>();
Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
//IEndpointInstance endpointInstance = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
//endpointInstance.Stop().ConfigureAwait(false);
//Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
//Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
//Log.CloseAndFlush();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
MessageHandler.cs
public class CustomerReceivedHandler : IHandleMessages<PubSubObject>
{
private readonly IDbProvider _DbProvider;
private static readonly ILog Log = LogManager.GetLogger<CustomerReceivedHandler>();
public CustomerReceivedHandler(IDbProvider DbProvider)
{
_DbProvider = DbProvider;
// If I don't inject and initialize as below, it works fine
//_DbProvider = new DbProvider(new ConfigSettings(new Encryptor());
}
public Task Handle(PubSubObject message, IMessageHandlerContext context)
{
Log.Info($"Received message with id {context.MessageId}");
}
}
答案 0 :(得分:0)
显然我应该使用以下代码:
endpointConfiguration.UseContainer<ServicesBuilder>(
customizations: customizations =>
{
customizations.ExistingServices(services);
});
我关注了https://docs.particular.net/samples/dependency-injection/aspnetcore/