为长文道歉!
我正在尝试在 aspnet core 2.1 中为 RabbitMQ 构建一个消息侦听器。我一发布消息,就会在日志中收到此错误:
2018-09-29 12:35:35.459 INFO NServiceBus.RecoverabilityExecutor Immediate Retry 将重试消息“ab43”,因为出现异常:
System.InvalidOperationException:无法解析类型:Event.Processor.Listener.CustomerReceivedHandler,服务名称:---> System.InvalidOperationException:未解析的依赖项
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>
启动.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);
}
}
程序.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>();
}
消息处理程序.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}");
}
}