C
C#10mo ago
huge

get scoped service

so i have a scoped serviced registered , since idk the generic type , so i used reflection get the types from assembly but how do i get the service like i cant pass the interface generic through services.BuildServiceProvider().GetRequiredService<>();
3 Replies
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
huge
hugeOP10mo ago
public static IServiceCollection RegisterCommandHandlers(this IServiceCollection services, Assembly assembly, Type compareType)
{
services.AddScoped<ICommandHandler<CreateCommand>, CreateCommandHandler>(); // i have multiple registrations , this is a sample one jic

CommandDispatcher dispatcher = new();
foreach (var commandHandlerType in assembly.GetTypes()
.Where(type => type.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>))))
{
var commandType = commandHandlerType.GetInterfaces()
.Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>))
.GetGenericArguments()[0];
Type genericType = typeof(ICommandHandler<>).MakeGenericType(commandType);
var commandHandler = services.BuildServiceProvider().GetRequiredService(genericType); //tried doing this beacuse idk what im doing

var handleAsyncMethod = commandHandlerType.GetMethod("HandleAsync");

if (handleAsyncMethod != null)
{
var handleDelegate = (Func<object, Task>)handleAsyncMethod.CreateDelegate(
typeof(Func<object, Task>).MakeGenericType(commandType, typeof(Task)),
commandHandler);

dispatcher.RegisterHandler(commandType, handleDelegate);
Console.Write("Registered a delegate");
}

else
{
Console.Write("here");
}
}

services.AddSingleton<ICommandDispatcher>(_ => dispatcher);
return services;

}
public static IServiceCollection RegisterCommandHandlers(this IServiceCollection services, Assembly assembly, Type compareType)
{
services.AddScoped<ICommandHandler<CreateCommand>, CreateCommandHandler>(); // i have multiple registrations , this is a sample one jic

CommandDispatcher dispatcher = new();
foreach (var commandHandlerType in assembly.GetTypes()
.Where(type => type.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>))))
{
var commandType = commandHandlerType.GetInterfaces()
.Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>))
.GetGenericArguments()[0];
Type genericType = typeof(ICommandHandler<>).MakeGenericType(commandType);
var commandHandler = services.BuildServiceProvider().GetRequiredService(genericType); //tried doing this beacuse idk what im doing

var handleAsyncMethod = commandHandlerType.GetMethod("HandleAsync");

if (handleAsyncMethod != null)
{
var handleDelegate = (Func<object, Task>)handleAsyncMethod.CreateDelegate(
typeof(Func<object, Task>).MakeGenericType(commandType, typeof(Task)),
commandHandler);

dispatcher.RegisterHandler(commandType, handleDelegate);
Console.Write("Registered a delegate");
}

else
{
Console.Write("here");
}
}

services.AddSingleton<ICommandDispatcher>(_ => dispatcher);
return services;

}
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server