ASP.核心反射di
本文关键字:di 反射 核心 ASP | 更新日期: 2023-09-27 18:03:40
我为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;
}
}
我在startup.cs
中使用它:
services.UseQueue<PrimaryEventHandlerImpl, IRsmService(
Configuration[AppConstants.HostPrimary],
Configuration[AppConstants.LoginPrimary],
Configuration[AppConstants.PasswordPrimary],
Configuration[AppConstants.QueuePrimary],
arg
);
但它在所有时间做错误在这一行:
var impl = (T)Activator.CreateInstance(queueType, parameters);
说:
没有找到
Monq.Core.Service.RSMModeGenerator.Services.PrimaryEventHandlerImpl
类型的构造函数。
但这是我的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();
}
}
}
我要怎么做才能让它正常工作?
变化
var impl = (T)Activator.CreateInstance(queueType, parameters);
var impl = (T)Activator.CreateInstance(queueType, parameters.ToArray());
你要做的是用一种简单而灵活的方式解决。
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;
}