What should I do to map all services dynamically in my grpc servicehost registration, instead of just mapping them one by one and editing this pieace of code everytime I add new service?
I've managed to get all types in assembly that are in GrpcServices and has my custom attribue using this:
public IEnumerable<Type> FindServiceInterfaces()
{
string definedIn = typeof(GrpcServiceAttribute).Assembly.GetName().Name;
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name.Equals("GrpcServices"));
foreach (Type type in assembly.GetTypes())
if (type.GetCustomAttributes(typeof(GrpcServiceAttribute), true).Length > 0)
yield return type;
}
But I don't know how to cast/instantiate those above types and use them as follow but in generic/dynamic way:
var serverHostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddGrpcServer<ISimpleActionService, SimpleActionService>(new GrpcServerOptions { Url = "127.0.0.1", Port = 5000 });
});
Desired solution: just iterate through all of the interfaces that has my given attribute, find class that implements this inteface IFoo -> Foo and add this -> services.AddGrpcServer<IFoo,Foo>(.....)
The only solution I found to instatiate those object is casting to already known type like this:
var desiredClass = (DesiredClassType) Activator.CreateInstance(assemblyType)
which is clearly not a solution for me.
Aucun commentaire:
Enregistrer un commentaire