[英]ASP.NET Core reflection di
I did settings for RabbitMQ, for example my extension: 我为RabbitMQ进行了设置,例如扩展名:
public static class QueueExtension
{
public static IServiceCollection UseQueue<TQueue, TApiService>(this IServiceCollection services, string host, string login,
string password, string queue, IDictionary<string, object> arg) where TQueue : class, IEventHandler where TApiService : class, IService
{
if (services == null)
throw new ArgumentNullException("services");
return UseQueue<TQueue, TApiService>(services, typeof(TQueue), typeof(TApiService), host, login, password, queue, arg);
}
public static IServiceCollection UseQueue<T, Y>(this IServiceCollection services, Type queueType, Type apiServiceType, string host, string login,
string password, string queue, IDictionary<string, object> arg) where T : IEventHandler where Y: IService
{
if (services == null)
throw new ArgumentNullException("services");
if (queueType == null)
throw new ArgumentNullException("queueType");
if (apiServiceType == null)
throw new ArgumentNullException("apiServiceType");
var parametersQueueType = queueType
.GetConstructors()
.SelectMany(p=>p.GetParameters())
.Select(p=>p.ParameterType)
.ToList();
var parametersType = services
.Where(p => parametersQueueType.Select(z => z.FullName)
.Contains(p.ServiceType.FullName))
.ToList();
var parameters = new List<object>();
parametersType.ForEach(p =>
{
parameters.Add((Y)Activator.CreateInstance(p.ImplementationType));
});
var impl = (T)Activator.CreateInstance(queueType, parameters);
var queueService = new QueueServiceImpl(host, login, password, queue, arg);
queueService.ReceivedMessage += (sender, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
impl.MessageHandler(message);
};
queueService.Start();
return services;
}
}
I use it in startup.cs
: 我在startup.cs
使用它:
services.UseQueue<PrimaryEventHandlerImpl, IRsmService(
Configuration[AppConstants.HostPrimary],
Configuration[AppConstants.LoginPrimary],
Configuration[AppConstants.PasswordPrimary],
Configuration[AppConstants.QueuePrimary],
arg
);
But it wrong in all time do error in this line: 但是它总是错误的在这一行做错误:
var impl = (T)Activator.CreateInstance(queueType, parameters);
Say that: 比如说:
Constructor ontype
Monq.Core.Service.RSMModeGenerator.Services.PrimaryEventHandlerImpl
not found. 找不到构造函数ontypeMonq.Core.Service.RSMModeGenerator.Services.PrimaryEventHandlerImpl
。
but this my PrimaryEventHandlerImpl.cs
: 但这是我的PrimaryEventHandlerImpl.cs
:
public class PrimaryEventHandlerImpl : IEventHandler
{
private IRsmService _rsmService;
public PrimaryEventHandlerImpl()
{
}
public PrimaryEventHandlerImpl(IRsmService rsmService)
{
_rsmService = rsmService;
}
public void MessageHandler(string json)
{
var events = JsonExtensions.JsonToObject<Event>(json);
if (events.Priority != 5)
{
//var services = _rsmService.GetServices();
}
}
}
What I want to do make it work correctly? 我要做什么才能使其正常工作?
Change 更改
var impl = (T)Activator.CreateInstance(queueType, parameters);
to 至
var impl = (T)Activator.CreateInstance(queueType, parameters.ToArray());
What u are trying to do resolves in much simple and flexible way. 您正在尝试做的事情以简单而灵活的方式解决。
static IServiceCollection UseQueue<T>(this IServiceCollection services,
RabbitMQOptions options)
where T : class, IEventHandler
{
if (services == null)
throw new ArgumentNullException("services");
if (options == null)
throw new ArgumentNullException("options");
services.AddTransient<T>();
services.AddTransient<IQueueService, QueueServiceImpl>();
services.AddSingleton<RabbitMQOptions>(sp => { return options; });
var serviceProvider = services.BuildServiceProvider();
var rabbitImpl = serviceProvider.GetService<T>();
var queueService = serviceProvider.GetService<IQueueService>();
queueService.ReceivedMessage += async (sender, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
await rabbitImpl.MessageHandler(message);
queueService.Channel.BasicAck(ea.DeliveryTag, false);
};
queueService.Start();
return services;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.